Calculating the difference
Calculating the difference
am 01.10.2007 15:49:15 von Julius
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
Does someone have a quick solution for this ?
Thanks ahead ...
Re: Calculating the difference
am 01.10.2007 16:05:19 von Erwin Moller
Julius wrote:
> 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 ..
Very simple?
>
> 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
>
> Does someone have a quick solution for this ?
> Thanks ahead ...
>
Why is the first simple and the second hard?
Anyway: go to www.php.net and look up: strtotime
Then use the difference in time to calculate the elapsed
suits you>.
Beware of timezone offset stuff. If it is not in the original date,
don't worry.
Regards,
Erwin Moller
Re: Calculating the difference
am 01.10.2007 16:11:31 von ragearc
On 1 Oct, 14:49, Julius wrote:
> 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
>
> Does someone have a quick solution for this ?
> Thanks ahead ...
First, http://pt.php.net/manual/en/function.strtotime.php. Then
$later_time - $earlier_time.
$date1 = '2005-12-25 00:56:27 GMT' ; // Note the timezone
specification
$time1 = strtotime($date1) ;
$date2 = '2005-12-27 05:56:27 GMT' ; // Note the timezone
specification
$time2 = strtotime($date2) ;
$difference = $time2 - $time1;
print date('z \d\a\y\s\, H:i:s', $difference - 3600); // X days,
Hours:Minutes:Seconds
I added - 3600 because it was showing 01:00:00 even when $difference
was 0. Probably DST thingie.
Re: Calculating the difference
am 01.10.2007 16:17:58 von Steve
"Julius" 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
there is no 'difference'. you want to subtract those hours/minutes/seconds
from 2007-11-06...right?
$stamp = strtotime('2007-11-06');
$minus = explode(':', '20:13:04');
foreach ($minus as $increment => $value)
{
switch($increment)
{
case 0: $increment = ' hours'; break;
case 1: $increment = ' minutes'; break;
case 2: $increment = ' seconds'; break;
}
$stamp = strtotime('-' . $value . $increment, $stamp);
}
Re: Calculating the difference
am 01.10.2007 16:24:15 von Steve
"Steve" wrote in message
news:4k7Mi.30$CY.1@newsfe06.lga...
>
> "Julius" 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.
Re: Calculating the difference
am 01.10.2007 19:42:36 von ragearc
On 1 Oct, 15:24, "Steve" wrote:
> "Steve" wrote in message
>
> news:4k7Mi.30$CY.1@newsfe06.lga...
>
>
>
>
>
> > "Julius" 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]
$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.