PHP: print a calendar fast


Say you need to fill a file or a sheet of paper (if you print the output) with a range of consecutive dates (or dates with equal space between them). Then, the following PHP one-liner might be handy some day:

for ($i=1; $i<=365; $i++) echo date( “Y-m-d”, mktime( 0, 0, 0, 1, $i)). “\n”;

The above snippet prints each day of current year on its own line. If you want one day per week, then you can change accordingly the values of for-loop variable, for example:

for ($i=4; $i<=365; $i+=7) […]

 Or, for next year’s calendar:

for ($i=1; $i<=365; $i++) echo date( “Y-m-d”, mktime( 0, 0, 0, 1, $i, 1+date(“Y”))). “\n”;

Don’t forget to change 365 with 366 for a leap year.

Visit The Light of the LAMP blog for more…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.