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

Copy link
Copy Markdown

Great example.

ghost commented Mar 1, 2018

Copy link
Copy Markdown

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

shahburhan commented Jun 7, 2018

Copy link
Copy Markdown

@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

tibelchior commented Jan 21, 2019

Copy link
Copy Markdown

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

rizkhal commented Nov 1, 2020

Copy link
Copy Markdown

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

pablim commented May 18, 2023

Copy link
Copy Markdown

Thanks

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