tags:

views:

156

answers:

5

Hi there

I have 2 unix timestamps, Im in AsiaPacific/Auckland timezone (GMT+12, DaylightSavings = GMT+13)

I want to calculate the number of days interval between 2 timestamps, where one is inside daylight savings time and one is not.

My example dates are

7 Feb 2009 (1233925200) to 21 September 2010 (1284985360) (not including 21st) see here it says 591 days: http://www.timeanddate.com/date/durationresult.html?d1=7&m1=2&y1=2009&d2=21&m2=9&y2=2010

Lets calculate, here are my timestamps (both are based on Auckland 00:00 time)

1284985360-1233925200 = 51060160
51060160 / 86400 = 590.974

So yea I need 591. I don't want to use the "round up" solution

Is there any reliable method like strtotime, but for calculating date intervals, preferably that dont need php 5.3+ minimum

EDIT: need to clarify, im using STRTOTIME to get these timestamps, I thought that was UTC

EDIT2: I believe i have found the issue. While my end date was 21 September, I was actually using time() to get my end date, and time() was returning the wrong timestamp, perhaps it doesnt account for the GMT+12, regardless I switched time() to strtotime(date('d M Y')) and it returned the correct timestamp! eureka 591 days

Cheers!

A: 

Dividing by 86400 is reliable and easy. I don't know any special functions and i don't see a reason for them to exist.

Māris Kiseļovs
In 5.3, new DateTime($t1)->diff(new DateTime($t2)) is easy and clear. But the OP asked for pre 5.3 a solution.
Colin Fine
what Daniel said
fatjoze
Which is why one should save timestamps in UTC.
You
dude im using STRTOTIME to get these timestamps, isn't that UTC
fatjoze
STRTOTIME has nothing to do with timezones.
mizipzor
@mizipzor: strtotime has very much to do with timezones - it converts a human readable date to a timestamp, and it has to use a specific timezone to interpret the date.
Michael Borgwardt
+2  A: 

A correct Unix (POSIX) timestamp is in UTC, so you're starting out with values that are already wrong. You're facing an uphill battle, since APIs will generally assume that timestamps are in UTC. It would be best to fix this, after which the simple division would actually give the correct result.

Michael Borgwardt
i have no idea if its UTC, im just runing STRTOTIME, so if you're saying strtotime generates a GMT instead of UNIX (POSIX) then yea guess i'm screwed. you seem knowledgable, i assume you're sayin there's no reliable way other than the 5.3+ methods???
fatjoze
@fatjoze: To be exact, it's wrong to say that strtotime generates a timestamp in a specific timezone. It always generates UTC, the question is what timezone it uses to interpret the human-readable date parameter. And it looks like that's incorrect in your case because the timestamps you list do not correspond to 00:00 in either UTC or Auckland time. You can set the default timezone via date_default_timezone_set()
Michael Borgwardt
@fatjoze: At second glance, your timestamps seem to correspond to 00:00 in Sydney - is it possible that that's where your servers are and what they're configured to use as default timezone?
Michael Borgwardt
thank you very very much Michael, very knowledgeable support, my timestamps SHOULD be Auckland New Zealand, i have used set_default_date_timezone (forgot the actual name) to set my timezone accordingly, so yea not sure why its showing sydney time. Do you live in sydney? lol. none the less I followed Gumbos solution to divide both numbers by 86400 then floor each, THEN subtract. It appears to work so far..
fatjoze
+2  A: 

Check out this link -

http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/

Alpesh
`date_diff` is not available before PHP 5.3.
Daniel Vandersluis
great stuff, not available pre 5.3 but oh well, still nice to have as reference!
fatjoze
+1  A: 

Calculate the number of full days for both timestamps before calculating the difference:

floor(1284985360 / 86400) - floor(1233925200 / 86400)

The your result is always an integer.

And since you’re using strtotime to get these timestamps, specify the time 00:00:00+0000 to always get a multiple of 86400:

strtotime($str.' 00:00:00+0000')
Gumbo
Only sensible solution so far. Good job downvoter.
Mchl
A: 

Gumbo good solution, it works, thankyou!!

fatjoez