RE: [PHP] selecting current month from a database
| From: | Ford, Mike | Date: | Mon, 22 May 2006 19:04:34 +0000 |
| Subject: | RE: [PHP] selecting current month from a database | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-236656@lists.php.net to get a copy of this message | ||
On , rabin.vincent@gmail.com wrote:
> I see Paul N. solved your selection problem. But I'm wondering
> why you need to store month names in a database. You could
> get them using date() and mktime() and avoid a db hit:
>
> foreach (range(1, 12) as $i) {
> echo date('F', mktime(0, 0, 0, $i));
> }
That's the right general idea, but as written has some specific problems:
(i) Using midnight when getting dates is not a good idea -- it can cause problems in some timezones
on DST shift dates. I always use midday when I'm interested purely in the date.
(ii) Omitting the day-of-month will give wrong answers on the 29th (non-leap years), 30th and 31st
of any month. I leave the reason behind this as an exercise for the reader... ;)
Therefore, I submit that a safer version of the above would be:
foreach (range(1, 12) as $i):
echo date('F', mktime(12, 0, 0, $i, 1));
endforeach;
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm