note 77767 added to function.time
| From: | kobieta dot ryba at gmail dot com | Date: | Wed, 12 Sep 2007 11:23:18 +0000 |
| Subject: | note 77767 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130414@lists.php.net to get a copy of this message | ||
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
)
----
Server IP: 194.145.210.4
Probable Submitter: 195.195.109.211
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/77767
Del: integrated -- https://master.php.net/note/delete/77767/integrated
Del: useless -- https://master.php.net/note/delete/77767/useless
Del: bad code -- https://master.php.net/note/delete/77767/bad+code
Del: spam -- https://master.php.net/note/delete/77767/spam
Del: non-english -- https://master.php.net/note/delete/77767/non-english
Del: in docs -- https://master.php.net/note/delete/77767/in+docs
Del: other reasons-- https://master.php.net/note/delete/77767
Reject -- https://master.php.net/note/reject/77767
Search -- https://master.php.net/manage/user-notes.php