DateTime修改当前第一天的字符串

Mar*_*rko 8 php datetime

我正在寻找一年中第一天的DateTime修改字符串(现在是2011年1月1日).我尝试了以下方法:

<?php

$time = new DateTime();

// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";


// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";

>
Run Code Online (Sandbox Code Playgroud)

我知道有其他方法可以检索日期,但我搜索字符串的DateTime-> modify()没有其他解决方案.

Aur*_*osa 12

您也应该指定年份,如您在此示例中所示:

"first day of January 2008"
Run Code Online (Sandbox Code Playgroud)

来自官方文件.

更新:适用于php版本> = 5.3.6


小智 8

在v5.5.6

回音日期('Ym-d',strtotime('今年1月的第一天'));

结果: 2013-01-01