I have this PHP code:
$monthNum = sprintf("%02s", $result["month"]);
$monthName = date("F", strtotime($monthNum));
echo $monthName;
But it's returning December
rather than August
.
$result["month"]
is equal to 8, so the sprintf
function is adding a 0
to make it 08
.
adapt as required
If you have the month number, you can first create a date from it with a default date of 1st and default year of current year, then extract the month name from the date created:
This code assume you have your month number stored in $i
Hope this helps someone
You need set fields with
strtotime
ormktime
With
mktime
set only month. Try this one:I found this on https://css-tricks.com/snippets/php/change-month-number-to-month-name/ and it worked perfectly.
Use:
If you just want an array of month names from the beginning of the year to the end e.g. to populate a drop-down select, I would just use the following;