Bogdan
Bogdan

Reputation: 1901

Datetime diff returning weird output

I'm using DateTime and diff to compute the difference in days between two dates as follows:

$data_inceput = '2013-10-01';
$data_sfarsit = '2013-10-31';
$date1 = new DateTime($data_inceput);
$date2 = new DateTime($data_sfarsit);

$nr_zile = $date2->diff($date1)->format("%a");

$nr_zile comes out as 6015 for some reasons.

If I `var_dump($date2->diff($date1));

i get:

object(DateInterval)#6 (8) {
   ["y"] => int(0)
   ["m"] => int(0)
   ["d"] => int(30)
   ["h"] => int(0)
   ["i"] => int(0)
   ["s"] => int(0)
   ["invert"] => int(1)
   ["days"] => int(6015)

Any idea why it's behaving this way? I've also tried to set the timezone to UTC to each date individually and get the same result.

edit: I tested on the production server and it works great, the only problem is on local apache server but the php settings are basically identical between the two.

Upvotes: 0

Views: 109

Answers (2)

Rajiv Ranjan
Rajiv Ranjan

Reputation: 1869

Alternate way you can use:

$data_inceput = strtotime('2013-10-01');
$data_sfarsit = strtotime('2013-10-31');
$daysBetween = round(($data_sfarsit - $data_inceput) / 86400);

Upvotes: 1

Harish Singh
Harish Singh

Reputation: 3329

this is a problem of Windows https://bugs.php.net/bug.php?id=51184

upgrade to the latest php version or use any alternate solution for this.

Upvotes: 2

Related Questions