Skip to content

Instantly share code, notes, and snippets.

@kosinix
Created April 10, 2013 13:30
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kosinix/5354637 to your computer and use it in GitHub Desktop.
Save kosinix/5354637 to your computer and use it in GitHub Desktop.
Get month names using a for loop and PHP date function.
<?php
for($m=1; $m<=12; ++$m){
echo date('F', mktime(0, 0, 0, $m, 1)).'<br>';
}
@aldiunanto
Copy link

works great. thanks!

@mshoaibdev
Copy link

it works , thank you man

@mehboobrana
Copy link

too simple and great work :)

@nasabikram
Copy link

Great example.

Copy link

ghost commented Mar 1, 2018

How to loop from December month of previous year to January month of next year?

@omusegad
Copy link

magic

@dasumenaria
Copy link

if i need to set this loop dec 2018 to march 2019 then how it works ??

@shahburhan
Copy link

shahburhan commented Jun 7, 2018

@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;
}

@tibelchior
Copy link

tibelchior commented Jan 21, 2019

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.

@sergeliatko
Copy link

Thanks saved me time

@flaviosilveira
Copy link

Amazing!

@rizkhal
Copy link

rizkhal commented Nov 1, 2020

thanks!

@supersuryaansh
Copy link

Thanks

@dhimaskirana
Copy link

thank you..

@bhargav944
Copy link

I want month from April to march

@mureithimaina
Copy link

Save me some time, thanks

@pablim
Copy link

pablim commented May 18, 2023

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment