【问题标题】:Date minus 1 year?日期减去 1 年?
【发布时间】:2010-12-31 17:04:05
【问题描述】:

我有一个这种格式的日期:

2009-01-01

我如何返回相同的日期但早于 1 年?

【问题讨论】:

  • 不要忘记关注你所说的“一年”w.r.t 的语义问题。闰年。从 2008-02-28 减去 365 天将得到 2007-02-28,而从 2008-02-29 减去 365 天将得到 2007-03-31。
  • 我想这在很大程度上取决于“减去一年”的含义。正如 Hostile 指出的那样,您可能指的是同一个月和一天,但一年前或减去 365 天后的一个月和一天。

标签: php date


【解决方案1】:

你可以使用strtotime:

$date = strtotime('2010-01-01 -1 year');

strtotime 函数返回一个 unix 时间戳,要获取格式化字符串,您可以使用 date

echo date('Y-m-d', $date); // echoes '2009-01-01'

【讨论】:

    【解决方案2】:

    使用 strtotime() 函数:

      $time = strtotime("-1 year", time());
      $date = date("Y-m-d", $time);
    

    【讨论】:

    • > date('Y-m-d', strtotime('-1 year'));
    【解决方案3】:

    使用 DateTime 对象...

    $time = new DateTime('2099-01-01');
    $newtime = $time->modify('-1 year')->format('Y-m-d');
    

    或者今天使用现在

    $time = new DateTime('now');
    $newtime = $time->modify('-1 year')->format('Y-m-d');
    

    【讨论】:

      【解决方案4】:

      我使用过并且效果很好的最简单方法

      date('Y-m-d', strtotime('-1 year'));
      

      这很完美..希望这对其他人也有帮助.. :)

      【讨论】:

        【解决方案5】:
        // set your date here
        $mydate = "2009-01-01";
        
        /* strtotime accepts two parameters.
        The first parameter tells what it should compute.
        The second parameter defines what source date it should use. */
        $lastyear = strtotime("-1 year", strtotime($mydate));
        
        // format and display the computed date
        echo date("Y-m-d", $lastyear);
        

        【讨论】:

          【解决方案6】:

          在我的网站上,为了检查注册人是否年满 18 岁,我简单地使用了以下内容:

          $legalAge = date('Y-m-d', strtotime('-18 year'));
          

          之后,只比较两个日期。

          希望它可以帮助某人。

          【讨论】:

            【解决方案7】:

            虽然对于这个问题有很多可以接受的答案,但我没有看到任何使用\Datetime 对象的sub 方法示例:https://www.php.net/manual/en/datetime.sub.php

            因此,作为参考,您还可以使用\DateInterval 来修改\Datetime 对象:

            $date = new \DateTime('2009-01-01');
            $date->sub(new \DateInterval('P1Y'));
            
            echo $date->format('Y-m-d');
            

            返回:

            2008-01-01
            

            有关\DateInterval的更多信息,请参阅文档:https://www.php.net/manual/en/class.dateinterval.php

            【讨论】:

              【解决方案8】:

              您可以使用以下函数从日期中减去 1 或任何年份。

               function yearstodate($years) {
              
                      $now = date("Y-m-d");
                      $now = explode('-', $now);
                      $year = $now[0];
                      $month   = $now[1];
                      $day  = $now[2];
                      $converted_year = $year - $years;
                      echo $now = $converted_year."-".$month."-".$day;
              
                  }
              
              $number_to_subtract = "1";
              echo yearstodate($number_to_subtract);
              

              看看上面的例子你也可以使用下面的

              $user_age_min = "-"."1";
              echo date('Y-m-d', strtotime($user_age_min.'year'));
              

              【讨论】:

                猜你喜欢
                • 2011-02-14
                • 1970-01-01
                • 2021-05-16
                • 2016-03-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多