Calculate days between date and today in PHP [duplicate]

为君一笑 提交于 2021-02-19 01:40:55

问题


If I have a given date in the format yyyy-mm-dd, how can I calculate the difference in days to the current date ?

I just want to check whether this date is more than one week (7 days) old compared to the current date.


回答1:


date_default_timezone_set('Europe/Warsaw');
$from = strtotime('2013-11-01');
$today = time();
$difference = $today - $from;
echo floor($difference / 86400);  // (60 * 60 * 24)

or

date_default_timezone_set('Europe/Warsaw');
$datetime1 = new DateTime('2013-11-01');
$datetime2 = new DateTime('2013-11-15');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a');


来源:https://stackoverflow.com/questions/20016224/calculate-days-between-date-and-today-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!