Hello there. I am kind of confused. In PHP I substract two unix timestamps from each other.

The first: mktime(0, 0, 0, 10, 22, 2012)
The second: mktime(24, 0, 0, 10, 28, 2012)

For as far as I know the time difference between these two timestamps is exactly 7 days. What I get, however, when I substract the second from the first ($time_difference = $second_timestamp - $first_timestamp) and then divide it by a week (60 * 60 * 24 * 7) is not 1, but 1.0059523809524 (which equals 1 week and 1 hour)!

Wtf?! Can someone please explain? :p

Edited by minitauros

5 Years
Discussion Span
Last Post by diafol

24 hours doesn't exist - it's 0!

Midnight 22/10/2012 to Midnight 29/10/2012 would be 7 days exactly.

The first: mktime(0, 0, 0, 10, 22, 2012)
The second: mktime(0, 0, 0, 10, 29, 2012)

BUT, daylight saving (UK anyway) ends on 28/10/2012, so you gain an extra hour!

See here: http://www.timeanddate.com/worldclock/clockchange.html?n=136

I've run into this many-a-time with calculations using unix timestamps, so you gotta be careful. DST settings and leap years can really mess your apps.

Edited by diafol


Yea I noticed :). But mktime(24, 0, 0, 10, 28, 2012) returned exactly the same as mktime(0, 0, 0, 10, 29, 2012), and both are not working.. :( I live in Holland and we have daylight saving time here as well, but wouldn't that mean that an hour should automatically be added to the first timestamp as well instead of only to the second one?

Edit: Oh nvm, I get it, daylight saving time ends just before my end date.. Right.. Thanks ^^! Good to know.

Edited by minitauros


Yeah, you got it. :)

BTW mktime() can utilize overflow and underflow - such as mktime(25,0,0, 10, 28, 2012) - which WILL work, and can be extremely useful - BUT this can often lead to confusion when dealing with '24', as (as you know) there is no 24:00:00.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.