Php 下个月的第一天

Php 下个月的第一天,php,date,timestamp,Php,Date,Timestamp,可能重复: 我必须在本月的第一天拿到这个 $_SERVER['dataInicio'] = date('Y') . "-" . date('m') . "-" . "1"; 我需要一些类似的东西来获得下个月的第一天这似乎是下个月的重复或类似帖子 但是在接下来的一个月里,如果你想使用strotime date("m/l/Y", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00')); 这会回来的 12

可能重复:

我必须在本月的第一天拿到这个

$_SERVER['dataInicio'] = date('Y') . "-" . date('m') . "-" . "1";

我需要一些类似的东西来获得下个月的第一天

这似乎是下个月的重复或类似帖子

但是在接下来的一个月里,如果你想使用strotime

date("m/l/Y", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));
这会回来的

12/Saturday/2012如果您只需要日期名称,只需将日期格式设置为l(小写l)

date(“l”,标准时间(date(“m”,标准时间(“+1个月”))。/01/”。date(“Y”)。“00:00:00”)

还是这个


应该做你想做的事。

2012年12月,下个月的第一天应该是2013-1年-1@CarlosMartins-该准则适用于任何月份。请参阅mktime文档:看起来不错,但是如果您在代码中添加一些注释,那么理解您的建议将更有帮助。最简单的方法是:$firstDayNextMonth=date('Y-m-d',strotime('下个月的第一天');我会这样做,它不会在十二月失败。标准时间(日期('m',标准时间('1个月'))。/01/'。日期('Y',标准时间('1个月'))@Halfstop很棒,我会更新我的答案我会在2017年12月15日运行此代码,并在2017年1月1日/周日返回,所以我回到12个月前,而不是2018年。因此,这种技术无法提供正确的输出。我找到了一个解决方案:$date_obj=date_create(“下个月的第一天”);echo$date=date_格式($date_obj,“Y-m-d H:i:s”);我使用了一种变体:日期('Y-m-01',标准时间('Y-m-d').+1个月'))标准时间('下个月的第一天');这不是最容易使用的东西吗?
$date = new DateTime('now');

$date->modify('first day of next month');
echo $date->format('D') . '\n';

$date->modify('first day of this month');
echo $date->format('D') . '\n';
$date = new DateTime('now');

$date->modify('first day of next month');
echo $date->format('D') . '\n';

$date->modify('first day of this month');
echo $date->format('D') . '\n';