如何从datetime.date()中减去datetime.datetime()

时间:2016-12-01 22:13:49

标签: datetime

我需要通过从datetime.datetime()中减去currentDate和日期来计算天数。 currentDate = datetime.date(2016,12,1) randomDate = datetime.datetime(2016,12,1,4,25)

numDays =(currentDate-randomDate)#but我不能这样做因为类型不同

1 个答案:

答案 0 :(得分:0)

您可以使用此方法查找两天的差异

$current_date = time(); // or your date as well
$random_date = strtotime("2010-01-01");
$datediff = $current_date - $random_date;

echo floor($datediff / (60 * 60 * 24));

PHP Version > 5以下添加新日期/时间功能以获得差异

$datetime1 = new DateTime("2010-06-20");

$datetime2 = new DateTime("2011-06-22");

$difference = $datetime1->diff($datetime2);

echo 'Difference: '.$difference->y.' years, ' 
                   .$difference->m.' months, ' 
                   .$difference->d.' days';

结果将如下:

Difference: 1 years, 0 months, 2 days