Re: convert numerical day of week

From: Date: Tue, 22 May 2007 17:39:32 +0000
Subject: Re: convert numerical day of week
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-255337@lists.php.net to get a copy of this message
On Tue, 2007-05-22 at 12:46 -0400, Bosky, Dave wrote: > How can I convert the numerical day of week to the string version? > > Example, if the day of the week is 1 I would like to print out 'Sunday'. I saw a bunch of bad examples given to you that completely ignore any available locale information... so here's a better version: <?php function dayIdToName( $id ) { static $days = null; if( $days === null ) { $control = gmmktime( 12, 1, 1, 5, 20, 2007 ); for( $i = 0; $i < 7; $i++ ) { $days[$i] = gmdate( 'l', $control + ($i * 24 * 60 * 60) ); } } return isset( $days[$id] ) ? $days[$id] : null; } ?> If you want Monday to be the first day of the week, just use a date for gmmktime() that falls on a Monday. Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#255337) next »