Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert year or month into days, PHP

Tags:

php

So I have used this method to get the difference between 2 dates.

$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

Now, lets say that I want to convert the years and months into days. How do I do that?

like image 919
Ayie Shindo Avatar asked Sep 20 '25 19:09

Ayie Shindo


1 Answers

Using DateTime this is a piece of cake:

$date1 = new DateTime($date1);
$date2 = new DateTime($date2);

$diff = $date1->diff($date2, true);

echo $diff->format('%a') . ' days';
like image 162
Ja͢ck Avatar answered Sep 22 '25 09:09

Ja͢ck