Skip to content

Instantly share code, notes, and snippets.

@kosinix
Created April 10, 2013 13:30
Show Gist options
  • Select an option

  • Save kosinix/5354637 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

works great. thanks!

@mshoaibdev
Copy link
Copy Markdown

it works , thank you man

@mehboobrana
Copy link
Copy Markdown

too simple and great work :)

@nasabikram
Copy link
Copy Markdown

Great example.

Copy link
Copy Markdown

ghost commented Mar 1, 2018

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

@omusegad
Copy link
Copy Markdown

magic

@dasumenaria
Copy link
Copy Markdown

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

@shahburhan
Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

Thanks saved me time

@flaviosilveira
Copy link
Copy Markdown

Amazing!

@rizkhal
Copy link
Copy Markdown

rizkhal commented Nov 1, 2020

thanks!

@supersuryaansh
Copy link
Copy Markdown

Thanks

@dhimaskirana
Copy link
Copy Markdown

thank you..

@bhargav944
Copy link
Copy Markdown

I want month from April to march

@mureithimaina
Copy link
Copy Markdown

Save me some time, thanks

@pablim
Copy link
Copy Markdown

pablim commented May 18, 2023

Thanks

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