如果我有2个日期, 21/05/2010 和 23/05/2010 ,我怎样才能知道 22/05/2006 7 :16 AM 存在于他们之间?
我使用以下代码计算最小/最大日期,然后将表中的SELECT所有记录清除以更新它们。
$today = date('l');
if($today == 'Wednesday'){
$min = date('d/m/Y', strtotime('0 days'));
$max = date('d/m/Y', strtotime('+6 days'));
}else if($today == 'Thursday'){
$min = date('d/m/Y', strtotime('-1 days'));
$max = date('d/m/Y', strtotime('+5 days'));
}else if($today == 'Friday'){
$min = date('d/m/Y', strtotime('-2 days'));
$max = date('d/m/Y', strtotime('+4 days'));
}else if($today == 'Saturday'){
$min = date('d/m/Y', strtotime('-3 days'));
$max = date('d/m/Y', strtotime('+3 days'));
}else if($today == 'Sunday'){
$min = date('d/m/Y', strtotime('-4 days'));
$max = date('d/m/Y', strtotime('+2 days'));
}else if($today == 'Monday'){
$min = date('d/m/Y', strtotime('-5 days'));
$max = date('d/m/Y', strtotime('+1 days'));
}else if($today == 'Tuesday'){
$min = date('d/m/Y', strtotime('-6 days'));
$max = date('d/m/Y', strtotime('0 days'));
}
答案 0 :(得分:4)
在 21/05/2010 和 22/05/2006 7:16 AM 之间创建一个DateTime :: diff,以及<之间的DateTime :: diff em> 23/05/2010 和 22/05/2006 7:16 AM 。
然后检查第一个DateTime :: diff是> 0
,第二个是< 0
更新:使用Datetime :: createFromFormat,这是一个php5.3方法
Update2 :经过测试的代码示例。产生预期的产出。
<?php
$datetime_lower = DateTime::createFromFormat('d/m/Y', '21/05/2010');
$datetime_upper = DateTime::createFromFormat('d/m/Y', '23/05/2010');
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', '22/05/2006 7:16 AM');
var_dump($datetime_lower < $datetime_compare);
var_dump($datetime_upper > $datetime_compare);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
echo " + date is between";
} else {
echo " date is not between";
}
此外,还有一个程序date_diff函数
答案 1 :(得分:2)
使用strtotime
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$datefind = strtotime($datefind);
if ($datefind >= $date1 && $datefind <= $date2)
答案 2 :(得分:1)
分解您的日期并使用mktime()中的部分来获取其时间戳值。然后,检查您的时间戳是大于还是小于其他时间是一件简单的事情。
http://php.net/manual/en/function.mktime.php
伪代码:
$parts= explode("/", "21/05/2010");
$timestamp1= mktime($parts[0], $parts[1], ...);
if($timestamp1 < $timestamp2...) {
print "timestamp1 is older then timestamp2";
}