This question already has an answer here:
this is my array value
$months = array(01 => "Jan", 02 => "Feb", 03 => "Mar", 04 => "Apr", 05 => "May", 06 => "Jun", 07 => "Jul", 08 => "Aug", 09 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec");
my output like this
Array ( [1] => Jan [2] => Feb [3] => Mar [4] => Apr [5] => May [6] => Jun [7] => Jul [0] => Sep [10] => Oct [11] => Nov [12] => Dec )
WHY?
</div>
If you want leading zeros you have to use string keys:
$months = array("01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec");
To show current month:
echo($months[date('m')]);
You have to remove the leading zeros in your array indices. Leading zeros in integer values define a number in octal
numbering format.
If you use 09
this means that you want to use 9
in octal numbering system, but that system only supports 0
- 7
, so that PHP will convert it to 0
.
Please change your code to:
$months = array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec");
Please also have a look at: int variable with leading zero?
You can try it.
$months = array("01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec");
$now = new \DateTime('now');
$month = $now->format('m');
echo $months[$month];