在PHP和MYSQL中比较两个日期

在PHP和MYSQL中比较两个日期

本文介绍了在PHP和MYSQL中比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较PHP中的两个日期和时间值。一个日期来自MySQL,第二个是当前日期。当两个日期相同时,我想运行一些代码。我尝试下面的代码,但条件满足任何时候是错误的。

I want to compare two dates and time values in PHP. One date is coming from MySQL, and second one is the current date. I want to run some code when both dates are the same. I tried the code below, but condition satisfies any time which is wrong.

$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
    //I want to run some code here
}else{
}

代码有什么问题?在比较之前,我也尝试使用 strtotime()隐藏这两个日期,但它给了我同样的问题。上述条件满足任何时间,即使两个日期不同。

What is wrong with the code? I also tried to covert both dates with strtotime() before comparing, but it gave me the same issue. The above condition satisfies any time even if both dates are different.

推荐答案

尝试这样:

$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if(strtotime($current_datetime) == strtotime($send_date)){
    //I want to run some code here
}else{
}

希望它帮助!!!!

这篇关于在PHP和MYSQL中比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:56