23
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);

How do i convert the above $interval to seconds in php

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
sanchitkhanna26
  • 2,143
  • 8
  • 28
  • 42
  • Note that neither minute nor hour is a precise unit of time. A minute may span 59, 60, or 61 seconds, an hour may contain 3599, 3600, or 3601 seconds. And converting months to seconds is complete nonsense. – matt Dec 22 '20 at 15:17

3 Answers3

49

Another way to get the number of seconds in an interval is to add it to the zero date, and get the timestamp of that date:

$seconds = date_create('@0')->add($interval)->getTimestamp();

This method will handle intervals created via the DateInterval contructor more or less correctly, whereas shiplu's answer will ignore years, months and days for such intervals. However, shiplu's answer is more accurate for intervals that were created by subtracting two dates. For intervals consisting only of hours, minutes and seconds, both methods will get the correct answer.

Community
  • 1
  • 1
Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • 1
    @Brilliand Why is this solution less accurate for intervals that were created by subtracting two dates? I don't understand how. Can you explain ? – MaxiWheat Oct 19 '15 at 19:32
  • 5
    @MaxiWheat Say the time interval was created by subtracting 2015-02-01 from 2015-03-01. That's a difference of one month, which happens to equal 28 days. Shiplu's method will treat this as a 28-day interval, whereas my method will treat it as a one-month interval, arbitrarily pick January as the month, and produce a timestamp based on the 31 days in January. – Brilliand Oct 20 '15 at 22:08
29

There is a function format for this. But it wont return the number of seconds. To get number of seconds you use this technique

$seconds = abs($datetime1->getTimestamp()-$datetime2->getTimestamp());

If you really want to use $interval you need to calculate it.

$seconds = $interval->days*86400 + $interval->h*3600 
           + $interval->i*60 + $interval->s;

Here

  • 86400 is the number of seconds in a day
  • 3600 is the number of seconds in an hour
  • 60 is the number of seconds in a minute
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • it is returning `0 seconds` – sanchitkhanna26 Jan 11 '13 at 11:56
  • @MarkBaker yes, thats why I changed my answer – Shiplu Mokaddim Jan 11 '13 at 11:59
  • This is still error prone. `$interval->days` _may_ be a boolean. This would be safer by using `$interval->y`, `$interval->m`, and `$interval->d` instead of `$interval->days`. The immediate problem you'll face is deciding how many days to use in a month. @Brilliand's answer fixes this, but has its own limitations as well. Choose wisely. – Joel Mellon Jan 31 '23 at 05:44
1

I would only add to shiplu's answer:

function dateIntervalToSeconds($interval)
{
    $seconds = $interval->days*86400 + $interval->h*3600 
       + $interval->i*60 + $interval->s;
    return $interval->invert == 1 ? $seconds*(-1) : $seconds;
}

To handle negative intervals.

Note that - contrary to Brilliand's answer - The code above will consider correctly years, months and dates. Because $interval->days is an absolute value ($interval->d is relative to the month).

EDIT: this function is still not correct, as pointed out by @Brilliand. A counter-example is

new DateInterval('P4M3DT2H'); 

It doesn't handle months well.

Alvaro Neira
  • 27
  • 1
  • 4
  • 1
    An example of an interval this will fail on is `new DateInterval('P4M3DT2H')`. Try it and see - the `days` variable is empty, despite the interval spanning roughly 123 days. – Brilliand Dec 29 '17 at 19:43
  • 1
    In the future, if you're going to accuse me of being wrong, I request that you do so in a comment on my answer. It took me a long time to notice this. – Brilliand Dec 29 '17 at 19:45
  • @Brilliand, you are right. I edited my answer accordingly. – Alvaro Neira Jan 04 '18 at 15:17
  • 1
    It isn't just months. Try `new DateInterval('P3D')` - the `days` variable is *still* empty. A DateInterval created with `new` just never fills in that variable. That's what I meant by "the DateInterval contructor". – Brilliand Jan 04 '18 at 22:43