我编写了一个脚本,我需要保存当前日期,以及该日期后1个月的日期。我很确定time()
变量有效,但我不确定如何+1 month
到那个?
任何想法,建议。干杯!
答案 0 :(得分:7)
试试这个
$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");
答案 1 :(得分:2)
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
这将返回一个可与日期函数
一起使用的时间戳答案 2 :(得分:1)
使用此:
当前日期:
echo "Today is " . date("Y/m/d");
当前日期的1个月:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
答案 3 :(得分:1)
<?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
?>
答案 4 :(得分:1)
更短:$ today = date(“Y-m-d”); $日期=
答案 5 :(得分:1)
这个班轮对我有用:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
答案 6 :(得分:0)
给出的答案可能无法为您带来预期或期望的结果。
考虑:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
答案 7 :(得分:0)
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
答案 8 :(得分:-1)
您也可以考虑使用 Carbon 包。
解决方案如下所示:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();