note 74713 added to function.time

From: Date: Tue, 24 Apr 2007 19:46:28 +0000
Subject: note 74713 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-125839@lists.php.net to get a copy of this message
I found Craig's code useful, but wanted the ability to limit the granularity to only show most significant time periods. there's the bit i adapted: <? function ago ( $epoch, $max_phrases=null ) { $duration = time() - $epoch; return seconds2human( $duration > 0 ? $duration : 0, $max_phrases) . " ago"; } function seconds2human($epoch, $max_phrases=null ){ // kindly adapted from Craig Francis at http://www.php.net/manual/en/function.time.php#74652 //-------------------------------------------------- // Maths $sec = $epoch % 60; $epoch -= $sec; $minSeconds = $epoch % 3600; $epoch -= $minSeconds; $min = ($minSeconds / 60); $hourSeconds = $epoch % 86400; $epoch -= $hourSeconds; $hour = ($hourSeconds / 3600); $daySeconds = $epoch % 604800; $epoch -= $daySeconds; $day = ($daySeconds / 86400); $week = ($epoch / 604800); //-------------------------------------------------- // Text $output = array(); if ($week > 0) { $output[] = $week . ' week' . ($week != 1 ? 's' : ''); } if ($day > 0) { $output[] = $day . ' day' . ($day != 1 ? 's' : ''); } if ($hour > 0) { $output[] = $hour . ' hour' . ($hour != 1 ? 's' : ''); } if ($min > 0) { $output[] = $min . ' minute' . ($min != 1 ? 's' : ''); } if ($sec > 0 || $output == '') { $output[] = $sec . ' second' . ($sec != 1 ? 's' : ''); } //-------------------------------------------------- // Grammar if( isset($max_phrases) ) $output = array_slice($output, 0, $max_phrases); $return = join( ', ', $output); $return = preg_replace('/, ([^,]+)$/', ' and $1', $return); //-------------------------------------------------- // Return the output return $return; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 209.202.205.1 ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- https://master.php.net/note/edit/74713 Del: integrated -- https://master.php.net/note/delete/74713/integrated Del: useless -- https://master.php.net/note/delete/74713/useless Del: bad code -- https://master.php.net/note/delete/74713/bad+code Del: spam -- https://master.php.net/note/delete/74713/spam Del: non-english -- https://master.php.net/note/delete/74713/non-english Del: in docs -- https://master.php.net/note/delete/74713/in+docs Del: other reasons-- https://master.php.net/note/delete/74713 Reject -- https://master.php.net/note/reject/74713 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125839) next »