[英]What is the 'best' way to check if a mySQL date is in the past in PHP?
所以我有一个存储日期时间值的数据库。 我想确定日期是否过去了-我不在乎这个实例中的时间部分,而只是日期。
所以我现在有以下代码
//this is the current date
$today = strtotime(date("Y-m-d"));
//this is the date from the database
$gameDate = strtotime($rec[0])
if (strtotime($rec[0]) < $today) {
我感觉自己并没有以最公认的方式进行操作,但是我的大脑因过多的数据而受伤,我希望有人可以为我提供“正确”方法的指导。
假设您只关心Date
部分,而不关心time
部分,例如2016-01-01 14:00:00
和2016-01-01 15:00:00
将被视为同一天而不是过去时间,您可以使用以下内容:
$dateFromDatabase = new DateTime('2016-05-23 17:00:00'); // $rec[0] in your case
$current = new DateTime();
$difference = $current->diff($dateFromDatabase);
if ($difference->format('%R') === '-') {
echo 'Date is in the past';
}
任何问题都让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.