Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP day of week numeric to day of week text

Tags:

date

php

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.

like image 867
csi Avatar asked Jan 20 '11 00:01

csi


People also ask

How do I get the day of the week in PHP?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How can get day name from day number in PHP?

Example 1: PHP Get Day Name from Date$newDate = date('l', strtotime($date)); echo $newDate; ?>


2 Answers

Bit of a hack, but:

$dow_text = date('D', strtotime("Sunday +{$dow_numeric} days")); 
like image 66
Hamish Avatar answered Sep 29 '22 08:09

Hamish


It's not popular, but there's actually a function jddayofweek for this in PHP. You can call it with the second parameter as 1 to get full gregorian week day name or 2 for the abbreviated name.

e.g. jddayofweek(2, 2); #returns Wed 

Note that for this function, numbers start at Monday. So Monday=0, Tuesday=1, ...

like image 34
Chibueze Opata Avatar answered Sep 29 '22 08:09

Chibueze Opata