how to get month name from month-year (08-2022) string in php
i want to get this Aug-2022 (format) from my value 08-2022
I had tried date('m-Y',strtotime(08-2022))
and date('08-2022')->format('m-Y')
but not working.
Advertisement
Answer
Just put a “01-” in front of your value to make it a valid date.
$value = "08-2022"; echo date('M-Y',strtotime('01-'.$value)); //Aug-2022
try self: https://3v4l.org/ZiB70
or with DateTime:
$value = "08-2022"; echo date_create_from_format('!m-Y',$value)->format('M-Y');
Important NOTE:
That ! in the format is important for this case and is unfortunately missing in many examples for date_create_from_format. If that ! missing then the current day is set as the day. If this happens on the 31st of a month and value is, for example, ’06-2022′ then a date ’31-06-2022′ is generated which is then reported as 01-07-2022. With the ! the day is set to 1 and the time to 00:00.