We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,459 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

# Confused: 7 * 24 hours equals 1 week 1 hour?

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

2
Contributors
3
Replies
1 Hour
Discussion Span
7 Months Ago
Last Updated
4
Views
Question
Answered
minitauros
Posting Whiz
333 posts since Apr 2011
Reputation Points: 45
Solved Threads: 51
Skill Endorsements: 4

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!

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.

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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.

minitauros
Posting Whiz
333 posts since Apr 2011
Reputation Points: 45
Solved Threads: 51
Skill Endorsements: 4
Question Answered as of 7 Months Ago by 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.

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

# This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help

You
View similar articles that have also been tagged:

© 2013 DaniWeb® LLC
Page rendered in 0.0663 seconds using 2.77MB