Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中比较日期时出现的奇怪问题_Php_Date_Strtotime - Fatal编程技术网

在php中比较日期时出现的奇怪问题

在php中比较日期时出现的奇怪问题,php,date,strtotime,Php,Date,Strtotime,我在用php比较两个日期时遇到了一个问题 问题:将上午12:00(午夜)与上午10:00(上午)进行比较时,以下代码将无法正常工作。按照人类的逻辑,上午10点比12点晚。但计算机似乎没有意识到这一点 有什么不同的方法吗 date_default_timezone_set('Europe/Athens'); $today = date("d-m-Y h:i:s A"); $today = date("d-m-Y h:i:s A",strtotime($today)); $max = date('d

我在用php比较两个日期时遇到了一个问题

问题:将上午12:00(午夜)与上午10:00(上午)进行比较时,以下代码将无法正常工作。按照人类的逻辑,上午10点比12点晚。但计算机似乎没有意识到这一点

有什么不同的方法吗

date_default_timezone_set('Europe/Athens');
$today = date("d-m-Y h:i:s A");
$today = date("d-m-Y h:i:s A",strtotime($today));
$max = date('d-m-Y h:i:s A', strtotime("31-12-2015 23:59:00"));
$min = date('d-m-Y h:i:s A', strtotime("14-12-2015 00:00:01"));

if (($today > $min) && ($today < $max)){
    //do something
} else {
    //something else done
}
date\u default\u timezone\u set(“欧洲/雅典”);
$today=日期(“d-m-Y h:i:SA”);
$today=日期(“d-m-Y h:i:s A”,标准时间($today));
$max=日期('d-m-Y h:i:s A',标准时间('31-12-2015 23:59:00');
$min=日期('d-m-Y h:i:s A',标准时间('14-12-2015 00:00:01”);
如果(($today>$min)和($today<$max)){
//做点什么
}否则{
//还有别的事吗
}
像这样试试

date_default_timezone_set('Europe/Athens');
            $today = date("d-m-Y h:i:s A");                
            $max = date('d-m-Y h:i:s A', strtotime("31-12-2015 23:59:00"));
            $min = date('d-m-Y h:i:s A', strtotime("14-12-2015 00:00:01"));

            if ((strtotime($today) > strtotime($min)) && (strtotime($today) < strtotime($max))){
                    //do something

            }else{
                    //something else done

//输出“做了其他事情”

您可以使用
date\u create()

$date1 = date_create("2013-03-15"); 
$date2 = date_create("2013-12-12");
$diff = date_diff($date1,$date2);
echo $diff->format("%R%a days");
因为
date()
返回字符串

从:

返回值 返回格式化的日期字符串。如果时间戳使用非数字值,则返回FALSE并发出E_警告级别错误


基本上,您的代码要做的是:

if("31-12-2015 23:59:00 PM" < "14-12-2015 00:00:01 AM") {
    .......
}

然后使用
date
将值重新格式化为AM/PM格式(如果需要)。

$today=date(“d-m-Y h:i:SA”)$今天=日期(“d-m-Y h:i:s A”,标准时间($today))
是如此的冗余即使这两行是完全冗余的。我已经删除了一行,我不建议将OOP和过程风格混合使用
if("31-12-2015 23:59:00 PM" < "14-12-2015 00:00:01 AM") {
    .......
}
$today = strtotime("now");
$max = strtotime("31-12-2015 23:59:00");
$min = strtotime("14-12-2015 00:00:01");

if (($today > $min) && ($today < $max)){
        //do something

}else{
        //something else done
}