如何在PHP中获得当前日期,并添加1个月到当前日期


How to get the current date in PHP, and add 1 month to the current date?

我正在编写一个脚本,我需要保存当前日期,日期从该日期1个月。我很确定time()变量工作,但我不确定如何+1 month到那?

有什么想法、建议吗?干杯!

试试这个

$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));

或使用DateTime()

$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));

(或)

strtotime( "+1 month", strtotime( $time ) );

返回一个时间戳,该时间戳可用于日期函数

使用

当前日期:

echo "Today is " . date("Y/m/d");

距离当前日期1个月:

$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month");  // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); //  Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose


?>

短:美元今天=日期("Y-m-d");$日期=

这一行对我很有用:

$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));

给出的答案可能不会给你期望的结果。

考虑:

$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018

$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));

您也可以考虑使用Carbon包。

解决方案是这样的:

use Carbon'Carbon
$now = Carbon::now;
$now->addMonth();

这里是供参考的链接https://carbon.nesbot.com/docs/