Ayie Shindo
Ayie Shindo

Reputation: 43

convert year or month into days, 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?

Upvotes: 4

Views: 6898

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173542

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';

Upvotes: 5

786
786

Reputation: 3

$currentDate = date("d-m-Y");
$date1 = date_create("".$joining_date."");
$date2 = date_create("".$currentDate."");
$diff12 = date_diff($date2, $date1);
$hub_days = $diff12->days;
$months = $diff12->m;
$years = $diff12->y;

Upvotes: 0

Related Questions