0

I have a function as follows:

function DateDiff($d1, $d2)
    {
        $date1 = new DateTime($d1);
        $date2 = new DateTime($d2);
        return $date1->diff($date2)->days; //return no of days
    }

I want to pass a third parameter something like this:

function DateDiff($d1,$d2,$convert){
}

and convert the result in seconds or minutes. Is there any default PHP function for that?

Thanks in advance!

amit roy
  • 3
  • 3
satroy
  • 116
  • 9
  • 1
    possible duplicate of [php - How To Convert 1 day To Seconds](http://stackoverflow.com/questions/20596906/php-how-to-convert-1-day-to-seconds) – kittykittybangbang Sep 01 '15 at 04:19
  • 1
    That duplicate doesn't have the DateTime answer you need, but the [manual](http://php.net/manual/en/datetime.diff.php#refsect1-datetime.diff-examples) [does](http://php.net/manual/en/class.dateinterval.php). – scrowler Sep 01 '15 at 04:30

1 Answers1

0

convert both the dates to seconds using strtotime() and then calculate its difference you will get the output in seconds itself

$diff_in_seconds = strtotime('datetime2') - strtotime('datetime2');

Arpita
  • 1,386
  • 1
  • 15
  • 35
  • Thanks for the idea :) but i was looking for if any default PHP functions available for this. Thanks! – satroy Sep 01 '15 at 05:43
  • 1
    No there is not you can get offset in seconds from a date, but you can not convert no of days to seconds. – Arpita Sep 01 '15 at 06:17