Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save earvinpiamonte/44de03cccf4c30a4877c4bccde09b3f7 to your computer and use it in GitHub Desktop.
Save earvinpiamonte/44de03cccf4c30a4877c4bccde09b3f7 to your computer and use it in GitHub Desktop.
Loop through numeric representation of months, with leading zeros in PHP
<?php
// get current timestamp
$timestamp = time();
// get full numeric representation of a year, 4 digits
$current_year = date('Y', $timestamp);
// maximum number of months to display
$max_monhts = 12;
for($i = 1; $i <= $max_monhts; $i++){
echo date('m', strtotime('01.'.$i.'.'.$current_year));
echo '<br/>';
}
// EXAMPLE OUTPUT
// 01
// 02
// 03
// 04
// 05
// 06
// 07
// 08
// 09
// 10
// 11
// 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment