RE: selecting current month from a database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On , rabin.vincent@xxxxxxxxx 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@xxxxxxxxxxxxxx
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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux