csi csi - 3 months ago 12
PHP Question

PHP day of week numeric to day of week text

This may be really easy but I can't find a PHP function to do this...

OK so

$dow_numeric = date('w');


gives the numeric day of the week 0-6 for Sunday to Saturday.

And

$dow_text = date('D');


gives the 3 letter abbreviation for the text day of the week (Sun, Mon, etc.)

Is there a function or easy way to use
$dow_numeric
to get
$dow_text
? If I have '0' as
$dow_numeric
, how can I make
$dow_text = 'Sun'
? Yes a
switch
statement could do the job, but I’m looking for a more elegant solution.

Answer

Create an array to map numeric DOWs to text DOWs.

$dowMap = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

If you need locale support, load the dow of some random date (epoch (0) would be a good date for example) and then for the next 6 days and build the dow map dynamically.

Comments