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

Recommended Answers

All 3 Replies

Member Avatar for 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.

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.

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.