Created
April 10, 2013 13:30
-
-
Save kosinix/5354637 to your computer and use it in GitHub Desktop.
Get month names using a for loop and PHP date function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
for($m=1; $m<=12; ++$m){ | |
echo date('F', mktime(0, 0, 0, $m, 1)).'<br>'; | |
} |
it works , thank you man
too simple and great work :)
Great example.
How to loop from December month of previous year to January month of next year?
magic
if i need to set this loop dec 2018 to march 2019 then how it works ??
@dasumenaria in that case you can do something like this:
<?php
$start_month = 12;
$end_month = 3;
$start_year = 2018;
for($m=$start_month; $m<=12; ++$m){
if($start_month == 12 && $m==12 && $end_month < 12)
{
$m = 0;
$start_year = $start_year+1;
}
echo date('F Y', mktime(0, 0, 0, $m, 1, $start_year)).'<br>';
if($m == $end_month) break;
}
This is a great solution, but if you need locale it's better to use strftime.
<?php
$currentMonth = date('n');
setlocale(LC_ALL,"de_DE.UTF8");
for($m=$currentMonth; $m<=12; ++$m){
$months[$m] = strftime('%B', mktime(0, 0, 0, $m, 1));
}
PS - this will return an array and will default the start to the current month.
Thanks saved me time
Amazing!
thanks!
Thanks
thank you..
I want month from April to march
Save me some time, thanks
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works great. thanks!