在php中的两个日期之间的比较

问题描述:

我有两个日期我想要在他们两个之间进行比较他们是在同一时间(年/月/日/小时) 我从数据库获取时间,其他时间从用户输入i已使用在php中的两个日期之间的比较

$checkdate = strtotime($meetingDate); 
$dateconflicts = strtotime($_SESSION['date']); 

if ($checkdate == $dateconflicts) { 
    $msg = "<div class='alert alert-danger'>there's conflicts !</div>";     
} 

事情是这样不行,我不知道为什么。

+0

'strtotime()'返回一个时间戳,表示您正在比较两个时间戳。所以除非你给出的日期时间完全相同,否则将失败。你可能想用日期格式化它,我推荐[DateTime](http://php.net/manual/en/datetime.construct.php)类。 – Qirel

+0

它是回答这个问题:http://*.com/questions/8722806/how-to-compare-two-dates-in-php –

+0

'$ _SESSION ['date']'的价值是什么在这里?会议开始了吗?编辑:'$ meetingDate'的值也是什么? –

要忽略秒,从时间只包括年,月,日的整数创建一个字符串,和小时:

$checkdate = date('Y-m-d H', strtotime($meetingDate)); 
$dateconflicts = date('Y-m-d H', strtotime($_SESSION['date'])); 

if ($checkdate == $dateconflicts) { 
    $msg = "<div class='alert alert-danger'>The dates $checkdate and $dateconflicts conflict !</div>"; 
} 
+0

我已经完成了你的解决方案,现在无论如何条件总是如此。 – ziyad

+0

请提供示例数据。 – WEBjuju

+0

@ziyad我更新了'$ msg',这样你就可以看到你的日期。如果您遇到意想不到的情况,那么问题可能出现在代码运行之前的日期。 – WEBjuju

如果你正在寻找如何检查2个日期具有相同的小时:

$date1 = '2016-12-04 20:21:10'; 
$date2 = '2016-12-04 21:50:00'; 

if (date('Y-m-d H', strtotime($date1)) == date('Y-m-d H', strtotime($date2))) { 
    echo 'Same hour'; 
} 
+1

我认为OP想要同一天,年份和月份。 '他们在同一时间(年/月/日/小时)' – chris85

+0

你是对的@ chris85,我更新了我的答案:) – Max

+1

OP在这里使用会话'$ _SESSION ['date']'所以我们不知道如果该数组包含一个值或者不包含它的值,那么是任何人的猜测。 –