How to get year and month from a date - PHP

后端 未结 10 1386
一个人的身影
一个人的身影 2020-12-24 06:37

How to get year and month from a given date.

e.g. $dateValue = \'2012-01-05\';

From this date I need to get year as 2012

10条回答
  •  鱼传尺愫
    2020-12-24 07:15

    I personally prefer using this shortcut. The output will still be the same, but you don't need to store the month and year in separate variables

    $dateValue = '2012-01-05';
    $formattedValue = date("F Y", strtotime($dateValue));
    echo $formattedValue; //Output should be January 2012
    

    A little side note on using this trick, you can use comma's to separate the month and year like so:

    $formattedValue = date("F, Y", strtotime($dateValue));
    echo $formattedValue //Output should be January, 2012
    

提交回复
热议问题