将年份或月份转换为天数,PHP

时间:2012-12-11 13:25:48

标签: php

所以我使用这种方法来区分两个日期。

$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));

现在,让我们说我希望将年份和月份转换为数天。我该怎么做?

2 个答案:

答案 0 :(得分:5)

使用DateTime这是小菜一碟:

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

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

echo $diff->format('%a') . ' days';

答案 1 :(得分:0)

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