How to get the first day of the current year?

后端 未结 11 1761
再見小時候
再見小時候 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:14

    If you want to get first day of current year just use this code

    echo date("l", strtotime('first day of January '.date('Y') ));
    
    0 讨论(0)
  • 2020-12-09 15:20

    Your relative date format 'first day of this year' is correct by returning the first day of the month because of the definition of first day of:

    Sets the day of the first of the current month. This phrase is best used together with a month name following it. (See PHP-doc)

    To get the first day of the current year with the relative format you can use something like this:

    'first day of January ' . date('Y')
    
    0 讨论(0)
  • You can get the current date and then set day and month to 1:

    $year = new DateTime();
    $year->setDate($year->format('Y'), 1, 1);
    

    Optionally, you can set the time to midnight:

    $year->setTime(0, 0, 0);
    
    0 讨论(0)
  • 2020-12-09 15:24

    Try this:

    $dt = date('m/d/Y',time());
    
    echo 'First day : '. date("01/01/Y", strtotime($dt)).' - Last day : '. date("m/t/Y", strtotime($dt)); 
    
    0 讨论(0)
  • 2020-12-09 15:25
    echo date('l',strtotime(date('Y-01-01')));
    
    0 讨论(0)
提交回复
热议问题