# Thread: subtracting timestamps from each other ok to work out time between them?

1. ## subtracting timestamps from each other ok to work out time between them?

if you subtract a timestamp from another one is that always going to work our right? i'm looking to get the number of days between two timestamps.

i'm wary to just subtract a timestamp from another because sometimes doing hard arithmatic with timestamps (hard arithmatic as in time()*\$x*24*60*60 where \$x is the number of days ahead of today you want) gives the wrong answer in some cases, like when you're in the period where day light savings change.

thanks

2. why don't you use mktime for more reliable issue ?

\$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));

3. thanks, but that doesn't do what i'm after. your suggestion starts out with a date and a number of days. i'm starting out with two dates.

4. The number of days between timestamps does not involve dailight savings, so it's OK do do that as long as both timestamps were originally obtained using the same timezone, ideally both timestamps should have been generated on the same server.

5. The number of days between timestamps does not involve dailight savings
are you sure on that? (why is it that this kind of thing is a such a mind bend?). daylight savings means one day of the year has 23 hours, another day 25 hours. so if i'm using time stamps to measure days i'd have to allow 23 hours to equal a day otherwise daylight saving could effect the number of days i think.

the time from say 6pm on one day to 6pm the next day, where daylight savings has changed means the number of seconds will be 23*60*60 or 25*60*60, depending whether the clocks went forward or back, between those two times.

i suppose what i'd have to do is first normalise any time (but not date) info in each time stamp to a fixed standard time, say 1pm, then second, after subtracting, round the answer up or down appropriately to obliterate any 23 hour or 25 hour days; in other words if the difference between the two timestamps is say 82800 (23*60*60) make sure that gets treated as a day. i think that'll do it. but i do think you're wrong saying "The number of days between timestamps does not involve dailight savings" but i could be wrong of course. thanks.

i've just done a test (on a uk server):

\$ts1 = strtotime('2010-10-30 18:00:00');
\$ts2 = strtotime('2010-10-31 18:00:00');
echo (\$ts2 - \$ts1) / (24*60*60);

outputs 1.04166666667

and

\$ts1 = strtotime('2010-03-27 18:00:00');
\$ts2 = strtotime('2010-03-28 18:00:00');
echo (\$ts2 - \$ts1) / (24*60*60);

outputs 0.958333333333

so if i use the same time for all dates being subtracted, and just round all number of day answers to no decimal places that should do it.

6. not checked but i think this'll do it

PHP Code:
``` /* returns number of days from the date of \$date1_ts to the date of \$date2_ts if both timestamp's dates are on the same day, returns 0 if \$date1_ts's date is after \$date2_ts's date, number of days negative */ function number_of_days_between_two_dates(\$date1_ts,\$date2_ts) {     \$ts1 = strtotime(date('Y-m-d',\$date1_ts).' 14:00:00');     \$ts2 = strtotime(date('Y-m-d',\$date2_ts).' 14:00:00');     return round( (\$ts2 - \$ts1) / (24*60*60) ); }  ```

7. Interesting, I guess it's better to use php's DateTime::diff since it's suppose to take care of daylight savings and timezones for you automatically
it's only available since php 5.3 though
http://us3.php.net/manual/en/datetime.diff.php

8. i didn't know about that. thanks for pointing that out.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•