PHP比较两个日期 [英] PHP compare two dates

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

问题描述

我有两个日期格式为YYYY-MM-DD,我想比较他们做一些任务。



我尝试了这段代码: p>

  $ temp_date = strtotime($ cu ['date']); 
$ temp_upto = strtotime($ upto_date);

if($ temp_date< = $ temp_upto){
echo< tr id ='highlight'>;
} else {
echo< tr>;
}

$ cu 一个数组,并包含相同格式的日期。这是比较php中的日期的正确方法吗?
我得到的输出是非常奇怪的!我没有发布完整的代码,因为它太长而且不相关于这个问题。
谢谢!



更新:所以这是我想要做的。我有很多正常的日期,如果正常日期小于或等于最新日期,我有一个最新的日期,那么我想添加id =highlight。对于我的代码的其他目的,整个事情都在循环中。



我回应了这样的日期:

  echoDATE:。 $ temp_date +UPTO:。 $ upto_date; 

,输出类似于

 日期:1349042400 UPTO:00-00-0000 
日期:1349388000 UPTO:00-00-0000
日期:1352588400 UPTO:00-00-0000
日期:1352761200 UPTO:00-00-0000
日期:1353193200 UPTO:00-00-0000
日期:1353366000 UPTO:1353193200
日期:1354143600 UPTO:1353193200
DATE :1354662000 UPTO:1353193200

仍然没有发生比较。我没有看到任何改变!



已经回答问题的人已经回答了问题,我已经阅读并实施了大部分的内容,没有任何成功。



更新2:虽然我仍然试图找出我的问题。我想在这里分享扩展的代码段 http://pastebin.com/nsVGV9dg

解决方案

不,这不对。你正在比较两个日期字符串。你想要比较时间戳:

  $ temp_dateTS = strtotime($ cu ['date']); 
$ temp_uptoTS = strtotime($ upto_date);

if($ temp_dateTS< = $ temp_uptoTS){
#code ...
}

如果您使用 DateTime 类:

  $ temp_date = new DateTime($ cu ['date']); 
$ temp_upto = new DateTime($ upto_date);

if($ temp_date< = $ temp_upto){
#code ...
}


I have two dates in the format YYYY-MM-DD and I want to compare them do do some task.

I tried this piece of code:

         $temp_date = strtotime($cu['date']);
         $temp_upto = strtotime($upto_date);

         if($temp_date <= $temp_upto){
            echo "<tr id='highlight'>"; 
        }else{
            echo "<tr>";
            }

$cu is an array and contains date in the same format. Is this the correct way to compare dates in php? The output which I get is quite weird! I haven't posted the full code because its too long and irrelevant for this question. Thanks!.

UPDATE: So this is what I am trying to do. I have many normal dates and I have a upto date if the normal dates are less than or equal to upto date then I want the id="highlight" added to . THe whole thing is inside a loop for some other purpose of my code.

I echoed the dates like this:

 echo "DATE: " . $temp_date + " UPTO: " . $upto_date;

and the output was something like

DATE: 1349042400 UPTO: 00-00-0000
DATE: 1349388000 UPTO: 00-00-0000
DATE: 1352588400 UPTO: 00-00-0000
DATE: 1352761200 UPTO: 00-00-0000
DATE: 1353193200 UPTO: 00-00-0000
DATE: 1353366000 UPTO: 1353193200
DATE: 1354143600 UPTO: 1353193200
DATE: 1354662000 UPTO: 1353193200

still the comparison doesn't happen. and I dont see any change!

People who are suggestion already answered questions, I have already read and implemented most of them without any success.

UPDATE 2: While I am still trying to figure out my issue. I want to share the expanded code snippet here http://pastebin.com/nsVGV9dg

解决方案

Nope, this isn't right. You're comparing two date strings. You want to compare the timestamps instead:

$temp_dateTS = strtotime($cu['date']);
$temp_uptoTS = strtotime($upto_date);

if ($temp_dateTS <= $temp_uptoTS) {
    # code...
}

This is also possible (and better) if you use DateTime class:

$temp_date = new DateTime($cu['date']);
$temp_upto = new DateTime($upto_date);

if ($temp_date <= $temp_upto) {
    # code...
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆