0

I am trying to convert a string to date. The strings i have look like these:

$string_jan = '01'; // 01 represents january
$string_feb = '02'; // 02 represents february
...
$string_dec = '12'; // 12 represents december

This works fine:

$string = '24-09-2020'; 
$date = strtotime($string); 
echo date('M', $date); // output Sep

But this does not work:

$string = '09'; // month september
$date = strtotime($string); 
echo date('M', $date); // output Jan ??

How can i convert this into the first 3 letters of a month using date?

john
  • 1,263
  • 5
  • 18
  • 1
    https://stackoverflow.com/questions/18467669/convert-number-to-month-name-in-php#:~:text=%24monthNum%20%3D%20sprintf(%22%25,0%20to%20make%20it%2008%20. – Axelle Sep 14 '20 at 09:42
  • How would PHP know if that number is a short year, month, day, hour, minute or second? – M. Eriksson Sep 14 '20 at 09:43
  • 1
    `strtotime` parses complete timestamps which are expected to contain a year, month, day and down to the second. It cannot guess what you want it to do with "09". – deceze Sep 14 '20 at 09:44
  • 1
    `$month = '09'; $dateObj = DateTime::createFromFormat('!m', $month); $conv_month = $dateObj->format('M'); echo $conv_month; // output Sep` – Jack Maessen Sep 14 '20 at 09:57

0 Answers0