我正在寻找一年中第一天的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";
>
我知道还有其他方法可以检索日期,但我搜索字符串的DateTime-&gt; modify()没有其他解决方案。
答案 0 :(得分:12)
答案 1 :(得分:8)
在v5.5.6上
回音日期('Y-m-d',strtotime('今年1月的第一天'));
结果:2013-01-01
答案 2 :(得分:2)
获取一年中第一天的星期几 或者是该月的第一天
<?php
//This is for a given month
$m="May";
// this id for this month
//$m=date('F');
//if you want the day of Sunday instead D use lower case l
echo date('D', strtotime('first day of January this year'));
echo "<br>". date("D", strtotime('first day of'. $m ));
?>
结果周五为五月与D
周三结果l
答案 3 :(得分:1)
这项工作对我来说(PHP 5.6 - 未在旧版本上测试)...当我们谈论DateTime
对象时
//Get current datetime
$now = new DateTime();
$now->modify('first day of January this year');
echo $now->format('Y-m-d');
// Print (current year)-01-01
答案 4 :(得分:0)
echo (new DateTime())->modify('first day of January this year')->format('Y-m-d');