简体   繁体   English

如何在PHP的DateTime中说“下个月15日”?

[英]How to say “next month 15th” in PHP's DateTime?

What I want to do: 我想做的事:

Getting a certain day from a certain month directly via the DateTime methods (no mktime stunts :)), like 通过DateTime方法直接从特定月份获得特定日期(无mktime特技:)),例如

$day = new DateTime('15th of next month');

but it's not possible to set a fixed day by it's number. 但是无法通过数字设置固定的日期。 Can anybody help ? 有人可以帮忙吗?

EDIT: I've changed the DateTime from this month to next month to make the problem more clear. 编辑:我已将DateTime从本月更改为下个月,以使问题更加清楚。

You can use DateTime::createFromFormat() . 您可以使用DateTime::createFromFormat() If you only pass the day value, it'll default to the current month and year. 如果仅传递day值,则默认为当前月份和年份。

$date = DateTime::createFromFormat('d', '15');

echo $date->format('Y-m-d'); // 2018-10-15

Edit for the new question requirements: 编辑新问题要求:

$date = DateTime::createFromFormat('d', 15)->add(new DateInterval('P1M'));

echo $date->format('Y-m-d'); // 2018-11-15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM