How to get the first day of the current year?

后端 未结 11 1834
再見小時候
再見小時候 2020-12-09 14:48

I need to use PHP DateTime to get the first day of the current year. I\'ve tried:

$year = new DateTime(\'first day of this year\');
var_dump($year);
<         


        
11条回答
  •  误落风尘
    2020-12-09 15:08

    in PHP 5.3.10 this works

    $myDate = new \DateTime(date("Y")."-01-01");                                                                                                                                                                        
    echo $myDate->format("Y-m-d");
    

    In PHP 5.4 and upper you can put all together

    echo (new \DateTime(date("Y")."-01-01"))->format("Y-m-d")
    

提交回复
热议问题