php date formatting for time difference
I just want to output as a result of a script to be execute开发者_Python百科d through CLI the time the execution of the script took.
for doing that, I set a var at the start of the script
$start_time = time();
and then at the end
date('H:i:s',time() - $start_time);
the problem is that even when the ellapsed time might be in the range of seconds or minutes, it always print that at least an hour has passed:
>>> echo date('H:i:s',1);
01:00:01
>>> echo date('H:i:s', 10);
01:00:10
>>> echo date('H:i:s',3599);
01:59:59
>>> echo date('H:i:s',3600);
02:00:00
shouldn't it display 00:XX:YY, when less than an hour has passed? is there something I'm missing, is there a bug?
thanks for your help!
Don't use date(). When you have time() - $start_time
the result is in seconds. Multiply this up if you want it in mintues etc. or use the following function to convert the seconds to Hours, Minutes and Seconds.
<?php /**
*
* @convert seconds to hours minutes and seconds
*
* @param int $seconds The number of seconds
*
* @return string
*
*/
function secondsToWords($seconds) {
/*** return value ***/
$ret = "";
/*** get the hours ***/
$hours = intval(intval($seconds) / 3600);
if($hours > 0)
{
$ret .= "$hours hours ";
}
/*** get the minutes ***/
$minutes = bcmod((intval($seconds) / 60),60);
if($hours > 0 || $minutes > 0)
{
$ret .= "$minutes minutes ";
}
/*** get the seconds ***/
$seconds = bcmod(intval($seconds),60);
$ret .= "$seconds seconds";
return $ret;
} ?>
Example usage:
<?php
/*** time since EPOCH ***/
echo secondsToWords(time());
?>
You could try this function:
<?
$time = strtotime('2010-04-28 17:25:43');
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
?>
From here: PHP How to find the time elapsed since a date time?
This will give you a better means of getting the difference. Simply replace strtotime('2010-04-28 17:25:43');
with whatever your start date/time is (as a timestamp). One of the benefits is the function can be repurposed elsewhere.
Try this:
echo "Time left:". date('H:i:s', (strtotime('2000-01-01 00:00:00') + (time()-$start_time) ));
Use gmdate() instead of date() to display the correct time difference and compensate for the server's time zone
This function gives a "fully" formatted human readable time string..
function humantime ($oldtime, $newtime = null, $returnarray = false) {
if(!$newtime) $newtime = time();
$time = $newtime - $oldtime; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$htarray = array();
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
$htarray[$text] = $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
$time = $time - ( $unit * $numberOfUnits );
}
if($returnarray) return $htarray;
return implode(' ', $htarray);
}
精彩评论