【问题标题】:How to compare two dates in php [duplicate]如何在php中比较两个日期[重复]
【发布时间】:2012-01-04 06:18:56
【问题描述】:

如果日期格式为 '03_01_12''31_12_11',如何在 php 中比较两个日期。

我正在使用此代码:

$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
   echo '1 is small ='.strtotime($date1).','.$date1;
else
   echo '2 is small ='.strtotime($date2).','.$date2;

但它不起作用..

【问题讨论】:

    标签: php


    【解决方案1】:

    您必须确保您的日期是有效的日期对象。

    试试这个:

    $date1=date('d/m/y');
    $tempArr=explode('_', '31_12_11');
    $date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
    

    然后您可以执行strtotime() 方法来获得差异。

    【讨论】:

    • 为什么不用str_replace?
    【解决方案2】:

    使用DateTime::createFromFormat

    $format = "d_m_y";
    $date1  = \DateTime::createFromFormat($format, "03_01_12");
    $date2  = \DateTime::createFromFormat($format, "31_12_11");
    
    var_dump($date1 > $date2);
    

    【讨论】:

    • 使用 d#m#Y 时,任何受支持的分隔符 ;, :, (, ), /, ., ,, - 都可以使用。
    【解决方案3】:

    不回答 OP 的实际问题,只回答标题。因为这是“比较 php 中的日期”的最佳结果。

    非常简单易用的日期时间对象 (php &gt;= 5.3.0) 和 Compare them directly

    $date1 = new DateTime("2009-10-11");
    $date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
    var_dump($date1 < $date2);
    

    【讨论】:

    • DateTime 类的对象无法转换为字符串
    【解决方案4】:

    date_diff() 函数返回两个 DateTime 对象之间的差异。

    如果第一个日期早于第二个日期,则返回正数天数;否则为负数:

    <?php
    $date1=date_create("2013-03-15");
    $date2=date_create("2013-12-12");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    ?>
    

    输出将是“+272 天”;

    改变 $date1 = "2014-03-15"

     <?php
        $date1=date_create("2014-03-15");
        $date2=date_create("2013-12-12");
        $diff=date_diff($date1,$date2);
        echo $diff->format("%R%a days");
        ?>
    

    输出将是“-93 天”

    【讨论】:

      【解决方案5】:
      <?php
             $expiry_date = "2017-12-31 00:00:00"
             $today = date('d-m-Y',time()); 
             $exp = date('d-m-Y',strtotime($expiry_date));
             $expDate =  date_create($exp);
             $todayDate = date_create($today);
             $diff =  date_diff($todayDate, $expDate);
             if($diff->format("%R%a")>0){
                   echo "active";
             }else{
                 echo "inactive";
             }
             echo "Remaining Days ".$diff->format("%R%a days");
      ?>
      

      【讨论】:

      • 你能补充一些解释吗?
      【解决方案6】:

      扩展@nevermind的答案,可以使用DateTime::createFromFormat:like,

      // use - instead of _. replace _ by - if needed.
      $format = "d-m-y";
      $date1  = DateTime::createFromFormat($format, date('d-m-y'));
      $date2  = DateTime::createFromFormat($format, str_replace("_", "-",$date2));    
      var_dump($date1 > $date2);
      

      【讨论】:

        【解决方案7】:

        你可以试试:

                $date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
                $date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
                $dateDiff = date_diff($date1, $date2);
                var_dump($dateDiff);
        

        然后您可以像这样 $dateDiff->d;

        【讨论】:

        • 嘿!谢谢,']'字符中有一个小错字,对吗?它靠近 Enter 键,我一定也按下了它。感谢您的关注。
        【解决方案8】:

        试试这个

        $data1 = strtotime(\date("d/m/Y"));
        $data1 = date_create($data1);
        $data2 = date_create("21/06/2017");
        
        if($data1 < $data2){
            return "The most current date is date1";
        }
        
        return "The most current date is date2";
        

        【讨论】:

          【解决方案9】:

          不知道你的问题是什么,但是:

          function date_compare($d1, $d2)
          {
              $d1 = explode('_', $d1);
              $d2 = explode('_', $d2);
              
              $d1 = array_reverse($d1);
              $d2 = array_reverse($d2);
              
              if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
              {
                  return $d2;
              }
              else
              {
                  return $d1;
              }
          }
          

          【讨论】:

            【解决方案10】:

            比较maketime()每次的结果

            【讨论】:

              【解决方案11】:

              我知道这已经晚了,但为了将来参考,请使用 str_replace 将日期格式转换为可识别的格式,然后您的函数将起作用。 (用破折号代替下划线)

              //change the format to dashes instead of underscores, then get the timestamp
              $date1 = strtotime(str_replace("_", "-",$date1));
              $date2 = strtotime(str_replace("_", "-",$date2));
              
              //compare the dates
              if($date1 < $date2){
                 //convert the date back to underscore format if needed when printing it out.
                 echo '1 is small='.$date1.','.date('d_m_y',$date1);
              }else{
                 echo '2 is small='.$date2.','.date('d_m_y',$date2);
              }
              

              【讨论】:

                【解决方案12】:

                您可以转换为整数并进行比较。

                例如:

                $date_1 = date('Ymd');
                $date_2 = '31_12_2011';
                
                $date_2 = (int) implode(array_reverse(explode("_", $date_2)));
                
                echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
                

                【讨论】:

                  【解决方案13】:

                  基准比较 date_create、strtotime、DateTime 和 direct

                  直接更快

                  $f1="2014-12-12";
                  $f2="2014-12-13";
                  $diff=false;
                  
                  
                  $this->bench('a');
                  for ($i=0; $i < 20000; $i++) { 
                      $date1=date_create($f1);
                      $date2=date_create($f2);
                      $diff=date_diff($date1,$date2);
                  }
                  $this->bench('a','b');
                  for ($i=0; $i < 20000; $i++) { 
                       $date1=strtotime($f1);
                       $date2=strtotime($f2);
                       if ($date1>$date2) {
                          $diff=true;
                      }
                  }
                  $this->bench('b','c');
                  for ($i=0; $i < 20000; $i++) { 
                      $date1 = new DateTime($f1);
                      $date2 = new DateTime($f2);
                      if ($date1>$date2) {
                          $diff=true;
                       }
                  }
                  $diff=false;
                  $this->bench('c','d');
                  for ($i=0; $i < 20000; $i++) { 
                       if ($f1>$f2) {
                          $diff=true;
                       }
                  }
                  $this->bench('d','e');
                  var_dump($diff);
                  
                  $this->dumpr($this->benchs);
                  
                  results:
                  
                      [a] => 1610415241.4687
                      [b] => 1610415242.8759
                      [a-b] => 1.407194852829
                      [c] => 1610415243.5672
                      [b-c] => 0.69137716293335
                      [d] => 1610415244.7036
                      [c-d] => 1.1363649368286
                      [e] => 1610415244.7109
                      [d-e] => 0.0073208808898926
                  

                  【讨论】:

                    【解决方案14】:

                    我觉得这个功能很简单

                    function terminateOrNotStringtoDate($currentDate, $terminationdate)
                    {
                        $crtDate = new DateTime($currentDate);
                        $termDate = new DateTime($terminationdate);
                        if($crtDate >= $termDate)
                        {
                            return true;
                        } else {
                        return false;
                        }
                    }
                    

                    【讨论】:

                      【解决方案15】:

                      各位,请不要这么复杂,下面的简单答案

                      $date1=date('d_m_y');
                      $date2='31_12_11';
                      $date1=str_replace('_', '-', $date1);
                      $date2=str_replace('_', '-', $date2)
                      if(strtotime($date1) < strtotime($date2))
                         echo '1 is small ='.strtotime($date1).','.$date1;
                      else
                         echo '2 is small ='.strtotime($date2).','.$date2;
                      

                      我刚刚在您的代码中添加了两行

                      【讨论】:

                        【解决方案16】:

                        如果两个日期的格式相同,则使用比较运算符。

                        $date1 = "2018-05-05"; 
                        $date2 = "2019-08-19"; 
                        
                        //comparison operator to  
                        
                        if ($date1 > $date2) {
                            echo "$date1 is latest than $date2"; 
                            }
                        else{
                            echo "$date1 is older than $date2"; 
                            }
                        

                        输出: 2018-05-05 早于 2019-08-19

                        【讨论】:

                        • 这仅在日期为 ISO 8601 格式时才有效。
                        猜你喜欢
                        • 2014-07-16
                        • 1970-01-01
                        • 2011-09-28
                        • 2013-11-24
                        • 2012-09-02
                        • 1970-01-01
                        • 2011-04-20
                        相关资源
                        最近更新 更多