By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

Calculating the difference

P: n/a
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 ...

Oct 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 <whatever unit
suits you>.

Beware of timezone offset stuff. If it is not in the original date,
don't worry.

Regards,
Erwin Moller
Oct 1 '07 #2

P: n/a
On 1 Oct, 14:49, Julius <julius.ehrl...@googlemail.comwrote:
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.

Oct 1 '07 #3

P: n/a

"Julius" <ju************@googlemail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.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);
}
Oct 1 '07 #4

P: n/a

"Steve" <no****@example.comwrote in message
news:4k***********@newsfe06.lga...
>
"Julius" <ju************@googlemail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.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.
Oct 1 '07 #5

P: n/a
On 1 Oct, 15:24, "Steve" <no....@example.comwrote:
"Steve" <no....@example.comwrote in message

news:4k***********@newsfe06.lga...


"Julius" <julius.ehrl...@googlemail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.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.

Oct 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.