PHP日期从今天到4个月

PHP date from today to 4 months

有谁知道为什么它一直显示日期 01-05-70?

$effectiveDate = strtotime("+4 months", strtotime($effectiveDate)); // returns timestamp
echo date('d-m-y',$effectiveDate); // formatted version

我希望它打印今天的日期 + 4 个月。

在您的代码中 $effectiveDate 包含无效日期。所以 strtotime() returns unix 纪元 1970 年 1 月 1 日。

但如果您只需要四个月后的日期,则根本不需要该变量。

echo date('d-m-y',strtotime('+4 months'));
$today = date('d-m-y');
$monthsFour = date('d-m-y',strtotime("+4 months"));
echo $today;
echo "<br>";
echo $monthsFour;

您在评论中的问题:Do you know how I express the year as 2016 as opposed to 16? 将日期函数中的 y 替换为大写 Y

已编辑:

$today = date('d-m-Y');
$monthsFour = date('d-m-Y',strtotime("+4 months"));
echo $today;
echo "<br>";
echo $monthsFour;

Does anyone know why this keeps on showing the date 01-05-70?

$effectiveDate 可能包含无效时间戳,我更喜欢使用 DateTime class,即:

$d1 = DateTime::createFromFormat('d-m-Y', '09-08-2016');
$d1->add(new DateInterval('P4M'));
echo $d1->format('d-m-Y');

Demo