[英]What is the 'best' way to check if a mySQL date is in the past in PHP?
So I have a database which stores a date-time value. 所以我有一个存储日期时间值的数据库。 I want to find out if the date is in the past - I don't care about the time part in this instance, just the date.
我想确定日期是否过去了-我不在乎这个实例中的时间部分,而只是日期。
So I have the following code at the moment 所以我现在有以下代码
//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) {
I feel like I am not doing this in the most accepted way, but my brain hurts from too much data and I was hoping someone could give me a pointer as to the 'correct'approach. 我感觉自己并没有以最公认的方式进行操作,但是我的大脑因过多的数据而受伤,我希望有人可以为我提供“正确”方法的指导。
Assuming you only care about the Date
part, and not the time
part, as in 2016-01-01 14:00:00
and 2016-01-01 15:00:00
would be counted as the same day and not the past, you can use the following: 假设您只关心
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';
}
ideone can be found here ideone可以在这里找到
Any issues let me know. 任何问题都让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.