Re: Difference between 2 time entries
| From: | tedd | Date: | Thu, 20 Jul 2006 15:54:04 +0000 |
| Subject: | Re: Difference between 2 time entries | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239638@lists.php.net to get a copy of this message | ||
At 1:51 PM +0200 7/20/06, nicolas figaro wrote:
>IMHO, the best is to generate a timestamp using mktime for each date.
>you can the calculate the difference of timestamps and convert it back using date.
>
>$tm_start = mktime(substr($start,0,2),substr($start,3,2), substr($start,5,2));
>$tm_finish = mktime(substr($finish,0,2),substr($finish,3,2), substr($finish,5,2));
>
>$tm_diff = $tm_finish -tm_start;
>
>print date("H:i",$tm_diff);
>
>I don't know if date accepts negative timestamps, so be sure $finish is later than $start
>(you can also put the day in case $finish = "00:00:12" and $start
>="15:00:00").
>
>hope this'll help
>
>N F
NF:
Not quite.
Your:
substr($start,5,2)
should be:
substr($start,6,2)
Your:
$tm_diff = $tm_finish -tm_start;
Should be:
$tm_diff = $tm_finish - $tm_start;
And lastly,
print date("H:i",$tm_diff);
Doesn't print the hours and seconds that have elapsed.
I thought that php had a function where you could send it a number of seconds and it would return
the number of years, months, days, hours and seconds, but I couldn't find any. But, date()
doesn't.
tedd
--
------------------------------------------------------------------------------------
http://sperling.com http://ancientstones.com http://earthstones.com