You are here: Re: Calculating the difference « PHP Programming Language « IT news, forums, messages
Re: Calculating the difference

Posted by Bruno Barros on 10/01/07 17:42

On 1 Oct, 15:24, "Steve" <no....@example.com> wrote:
> "Steve" <no....@example.com> wrote in message
>
> news:4k7Mi.30$CY.1@newsfe06.lga...
>
>
>
>
>
> > "Julius" <julius.ehrl...@googlemail.com> wrote in message
> >news:1191246555.184747.240030@n39g2000hsh.googlegroups.com...
> >> Hej dudes,
>
> >> I need to calc the difference between two timestamps / dates ...
>
> >> For example what i need to calculate:
>
> >> Date 1: 2007.11.06 - 20:13:04
> >> Date 2: 2007.11.07 - 21:13:04
>
> >> Difference: 1 day, 1hour
>
> >> Very simple in php ..
>
> >> but how do i calculate the difference between the following values:
>
> >> Date 1: 2007.11.06 - 20:13:04
> >> Date 2: 2007.11.08 - 03:13:04
>
> sorry, didn't take you oddball date notation as a single date stamp but as
> the calculation. anyway, bruno's works fine.

It might work fine but I found out it only goes to 364 days of
distance, so here's a new code, completely fixed for that bug:

[PHP]
<?php
$date1 = '2005-12-25 00:56:27 GMT' ; // Note the timezone
specification
$date2 = '2005-12-27 05:56:27 GMT' ; // Note the timezone
specification
$time_fix = 3600;

$time1 = strtotime($date1);
$time2 = strtotime($date2) ;
$difference = $time2 - $time1;
$years = date('Y', $difference) - 1970;

print "$years years, ";
print date('z \d\a\y\s\, H:i:s', $difference - $time_fix); // X days,
Hours:Minutes:Seconds
?>
[/PHP]

I removed the 1970 because that was the default value, so if the
distance is 0 years, then it will show up as 1970 - 1970 which is 0.

Also, in $time_fix, you can edit the number of seconds to remove from
the difference, so if you don't need to remove 3600 or whatever, you
can edit that easily.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация