Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Convert timestamp to months and days

How can I convert a timestamp difference between 2 dates

$diff = abs(strtotime($date2) - strtotime($date1));

to months and days quantity as desired ouput:

$res = 4.05 //-> 4 months and 5 days
like image 685
Blackbeard Avatar asked Nov 29 '22 19:11

Blackbeard


1 Answers

Something like this would possibly work. My maths may be a bit off.

$diff = abs(strtotime($date2) - strtotime($date1));

define('DAY',60*60*24, true);
define('MONTH',DAY*30, true);
define('YEAR',DAY*365, true);

$years = floor($diff / (YEAR));
$months = floor(($diff - $years * YEAR) / (MONTH));
$days = floor(($diff - $years * YEAR - $months*MONTH ) / (DAY));
like image 113
Yacoby Avatar answered Dec 05 '22 22:12

Yacoby