在php中的两个日期之间的比较
问题描述:
我有两个日期我想要在他们两个之间进行比较他们是在同一时间(年/月/日/小时) 我从数据库获取时间,其他时间从用户输入i已使用在php中的两个日期之间的比较
$checkdate = strtotime($meetingDate);
$dateconflicts = strtotime($_SESSION['date']);
if ($checkdate == $dateconflicts) {
$msg = "<div class='alert alert-danger'>there's conflicts !</div>";
}
事情是这样不行,我不知道为什么。
答
要忽略秒,从时间只包括年,月,日的整数创建一个字符串,和小时:
$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>";
}
答
如果你正在寻找如何检查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';
}
'strtotime()'返回一个时间戳,表示您正在比较两个时间戳。所以除非你给出的日期时间完全相同,否则将失败。你可能想用日期格式化它,我推荐[DateTime](http://php.net/manual/en/datetime.construct.php)类。 – Qirel
它是回答这个问题:http://*.com/questions/8722806/how-to-compare-two-dates-in-php –
'$ _SESSION ['date']'的价值是什么在这里?会议开始了吗?编辑:'$ meetingDate'的值也是什么? –