【问题标题】:$date + 1 year?$日期 + 1 年?
【发布时间】:2021-11-20 00:07:15
【问题描述】:

我正在尝试获取距我指定日期一年的日期。

我的代码如下所示:

$futureDate=date('Y-m-d', strtotime('+one year', $startDate));

它返回了错误的日期。任何想法为什么?

【问题讨论】:

  • 你忘了告诉错误。
  • Frank Farmer:你就这么确定吗?我宁愿等待 OP 的 retifications/cmets。
  • 昨晚匆忙发布此消息,我忘记澄清 - 它返回了错误的日期。对不起!感谢您的帮助。
  • 改为这样使用 strtotime('+1 year', $startDate));

标签: php strtotime


【解决方案1】:
$futureDate=date('Y-m-d', strtotime('+1 year'));

$futureDate 是一年后!

$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)) );

$futureDate 距离 $startDate 一年!

【讨论】:

  • 请注意,如果 $startDate 已经是时间戳(例如之前使用 time() 或 mktime() 函数创建的时间戳)而不是字符串,则不应将其包装在 strtotime 中,否则它将不行。相反,请按照下面提到的 K Prime 执行 $futureDate=date('Y-m-d',strtotime('+1 year',$startDate));
【解决方案2】:

要将一年添加到今天的日期,请使用以下命令:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));

对于其他示例,您必须使用时间戳值初始化 $StartingDate 例如:

$StartingDate = mktime();  // todays date as a timestamp

试试这个

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 365 day"));

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 1 year"));

【讨论】:

  • 添加“+365”而不是“+1 年”做到了。谢谢!
  • 如果是闰年,这不会中断吗?
  • 从 PHP 5.1 开始,当不带参数调用时,mktime() 会抛出 E_STRICT 通知:请改用 time() 函数。
  • 这里也被否决了。一天中的秒数、一年中的天数、一年中的秒数等都是可变的。如果您对“一年后”感兴趣,则必须通过“一年”的间隔。自行细分会导致闰日和闰秒以及夏令时变化的错误。更糟糕的是,它会导致不稳定的单元测试。
  • 为什么我的回答被否决了?? $addoneyear=date("y")+1; //加一年有什么问题?
【解决方案3】:
//1 year from today's date
echo date('d-m-Y', strtotime('+1 year'));

//1 year from from specific date
echo date('22-09-Y', strtotime('+1 year'));

希望这个更简单的代码对将来的人有所帮助:)

【讨论】:

    【解决方案4】:

    试试:$futureDate=date('Y-m-d',strtotime('+1 year',$startDate));

    【讨论】:

    • 虽然确实返回了正确答案,但另一个实际上也没有返回错误......只是错误的日期。
    • strtotime 不会抛出错误。出错时返回 false。
    • 如果未设置默认时区,PHP 将抛出警告......虽然显然这不是他的意思
    • 我的代码出错了我将 , 更改为 . 并且它有效 date("Y-m-d",strtotime('+1 year '.$startDate));
    【解决方案5】:

    刚刚遇到同样的问题,但这是最简单的解决方案:

    <?php (date('Y')+1).date('-m-d'); ?>
    

    【讨论】:

    • 太好了,我用它来获取明年:$next_year = (date('Y')+1).
    【解决方案6】:
    // Declare a variable for this year 
    $this_year = date("Y");
    // Add 1 to the variable
    $next_year = $this_year + 1;
    $year_after = $this_year + 2;
    
    // Check your code
        echo "This year is ";
        echo $this_year;
        echo "<br />";
        echo "Next year is ";
        echo $next_year;
        echo "<br />";
        echo "The year after that is ";
        echo $year_after;
    

    【讨论】:

      【解决方案7】:

      我更喜欢 OO 方法:

      $date = new \DateTimeImmutable('today'); //'today' gives midnight, leave blank for current time.
      $futureDate = $date->add(\DateInterval::createFromDateString('+1 Year'))
      

      使用DateTimeImmutable 否则你也会修改原来的日期! 更多关于 DateTimeImmutable:http://php.net/manual/en/class.datetimeimmutable.php


      如果您只想从今天开始,那么您可以随时这样做:

      new \DateTimeImmutable('-1 Month');
      

      【讨论】:

        【解决方案8】:

        如果你使用的是PHP 5.3,那是因为你需要设置默认时区:

        date_default_timezone_set()
        

        【讨论】:

          【解决方案9】:

          strtotime() 正在返回bool(false),因为它无法解析字符串'+one year'(它不理解“一”)。然后false 被隐式转换为integer 时间戳0。最好先验证strtotime() 的输出不是bool(false),然后再将其推入其他函数。

          From the docs:

          返回值

          成功返回时间戳,FALSE 否则。在 PHP 5.1.0 之前,这个 函数会在失败时返回 -1。

          【讨论】:

          • 是的,好点。我的生产代码会有这个,但我正在努力让它工作,所以把它剥离成尽可能少的代码。谢谢!
          【解决方案10】:

          试试这个

          $nextyear  = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)),   date("d",strtotime($startDate)),   date("Y",strtotime($startDate))+1));
          

          【讨论】:

            【解决方案11】:

            还有一个更简单、不太复杂的解决方案:

            $monthDay = date('m/d');
            $year = date('Y')+1;
            $oneYearFuture = "".$monthDay."/".$year."";
            echo"The date one year in the future is: ".$oneYearFuture."";
            

            【讨论】:

              【解决方案12】:

              我的解决方案是:date('Y-m-d', time()-60*60*24*365);

              您可以通过定义使其更具“可读性”:

              define('ONE_SECOND', 1);
              define('ONE_MINUTE', 60 * ONE_SECOND);
              define('ONE_HOUR',   60 * ONE_MINUTE);
              define('ONE_DAY',    24 * ONE_HOUR);
              define('ONE_YEAR',  365 * ONE_DAY);
              
              date('Y-m-d', time()-ONE_YEAR);
              

              【讨论】:

                【解决方案13】:

                就我而言(我想在当前日期加上 3 年),解决方案是:

                $future_date = date('Y-m-d', strtotime("now + 3 years"));
                

                致 Gardenee、Treby 和 Daniel Lima: 2月29日会发生什么?有时二月只有 28 天 :)

                【讨论】:

                  【解决方案14】:

                  您可以使用 strtotime() 获取未来时间。

                  //strtotime('+1 day');
                  //strtotime('+1 week');
                  //strtotime('+1 month');
                  
                   $now = date('Y-m-d'); 
                   $oneYearLaterFromNow = date('Y-m-d', strtotime('+1 year'));
                   $oneYearLaterFromAnyDate = date('Y-m-d', strtotime('+1 year', strtotime($anyValidDateString)));
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2010-12-31
                    • 1970-01-01
                    • 2011-04-01
                    • 1970-01-01
                    • 2021-07-13
                    • 2017-06-08
                    • 2020-06-18
                    相关资源
                    最近更新 更多