note 77767 deleted from function.time by aidan

From: Date: Wed, 15 Apr 2009 16:04:28 +0000
Subject: note 77767 deleted from function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153330@lists.php.net to get a copy of this message
Note Submitter: kobieta dot ryba at gmail dot com ---- Time left function: <?php define("TIME_PERIODS_PLURAL_SINGULAR", "weeks:week,years:year,days:day,hours:hour, : ,minutes:minute,seconds:second"); DEFINE("TIME_LEFT_STRING_TPL", " #num# #period#"); /** * @param $time time stamp **/ function time_left($time) { if (($now = time()) <= $time) return false; $timeRanges = array('years' => 365*60*60*24,/* 'weeks' => 60*60*24*7, */ 'days' => 60*60*24, 'hours' => 60*60, 'minutes' => 60, 'seconds' => 1); $secondsLeft = $now-$time; // prepare ranges $outRanges = array(); foreach ($timeRanges as $period => $sec) if ($secondsLeft/$sec >= 1) { $outRanges[$period] = floor($secondsLeft/$sec); $secondsLeft -= ($outRanges[$period] * $sec); } // playing with TIME_PERIODS_PLURAL_SINGULAR $periodsEx = explode(",", TIME_PERIODS_PLURAL_SINGULAR); $periodsAr = array(); foreach ($periodsEx as $periods) { $ex = explode(":", $periods); $periodsAr[$ex[0]] = array('plural' => $ex[0], 'singular' => $ex[1]); } // string out $outString = ""; $outStringAr = array(); foreach ($outRanges as $period => $num) { $per = $periodsAr[$period]['plural']; if ($num == 1) $per = $periodsAr[$period]['singular']; $outString .= $outStringAr[$period] = str_replace(array("#num#", "#period#"), array($num, $per), TIME_LEFT_STRING_TPL); } return array('timeRanges' => $outRanges, 'leftStringAr' => $outStringAr, 'leftString' => $outString); } print_r(time_left(time()-60*60*24*365+59)); ?> Output: Array ( [timeRanges] => Array ( [days] => 364 [hours] => 23 [minutes] => 59 [seconds] => 1 ) [leftStringAr] => Array ( [days] => 364 days [hours] => 23 hours [minutes] => 59 minutes [seconds] => 1 second ) [leftString] => 364 days 23 hours 59 minutes 1 second )

« previous php.notes (#153330) next »