Created
September 1, 2019 04:56
-
-
Save earvinpiamonte/44de03cccf4c30a4877c4bccde09b3f7 to your computer and use it in GitHub Desktop.
Loop through numeric representation of months, with leading zeros in PHP
This file contains 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 | |
// 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