开发者

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开发者_Python百科 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.


Bit of a hack, but:

$dow_text = date('D', strtotime("Sunday +{$dow_numeric} days"));


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, ...


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.


To get Sunday to Saturday from numeric day of the week 0 to 6:

//For example, our target numeric day is 0 (Sunday):
$numericDay = 0; //assuming current date('w')==0 and date('D')=='Sun';

Solution-1: Using PHP's built-in function jddayofweek() which starts from Monday whereas date('w') starts from Sunday:

jddayofweek($numericDay-1, 1); //returns 'Sun', here decreasing by '-1' is important(!)

//jddayofweek(0, 1); //returns 'Mon';
//jddayofweek(0, 2); //returns 'Monday';

Solution-2: Using a trick(!):

date('D', strtotime("Sunday +{$numericDay} days")); //returns 'Sun';

//date('l', strtotime("Sunday +{$numericDay} days")); //returns 'Sunday';


function getDay(){
    $dowMap = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    $dow_numeric = date('w');
    return $dowMap[$dow_numeric];
}


If you need locale support you can use a bit improved function of Hamishs answer:

Example using german weekdays:

setlocale(LC_TIME, 'de_DE');
$dow_numeric = 3;
$dow_text = strftime('%A', strtotime("Sunday +{$dow_numeric} days"));

http://php.net/strftime

If $dow_numeric start on 0 (0 = Monday) you can just change Sunday to Monday or add $dow_numeric += 1


This should work:

$dow_numeric = 3;

$last_sunday = strtotime('last Sunday');
$dow_text = date('D', strtotime('+'.$dow_numeric.' day', $last_sunday));


For an object oriented approach you can use DateTime('Sunday')::add() with a DateInterval of "PxD"

When using the standard Numeric representation of the day of the week DateTime::format('w') as 0-6 for Sun-Sat or ISO-8601 numeric representation of the day of the week DateTime::format('N') as 1-7 for Mon-Sun.
https://3v4l.org/3uthm

$weekday = (new DateTime('Sunday'))
    ->add(new DateInterval('P' . $dow . 'D'));

The ISO-8601 representation works as expected since it uses Sunday + 7 days (Sunday), resulting in Sunday + 1 day (Monday) and Sunday + 6 days (Saturday).

Alternatively replace Sunday for Monday and use $dowISO - 1
https://3v4l.org/5uLOL

$weekdayISO = (new DateTime('Monday'))
    ->add(new DateInterval('P' . ($dowISO - 1) . 'D'));

DatePeriod Example

https://3v4l.org/uG9NT

$sunday = new DateTimeImmutable('Sunday');
$monday = new DateTimeImmutable('Monday');
foreach (new DatePeriod($sunday, new DateInterval('P1D'), 6) as $date) {
    $dow = $date->format('w');
    $weekday = $sunday->add(new DateInterval('P' . $dow . 'D'));

    $dowISO = $date->format('N');
    $weekdayISOM = $monday->add(new DateInterval('P' . ($dowISO - 1) . 'D'));
    $weekdayISOS = $sunday->add(new DateInterval('P' . $dowISO . 'D'));

    printf("STD:  %s = %s\nISOM: %s = %s\nISOS: %s = %s\n\n", 
        $dow, $weekday->format('l'), 
        $dowISO, $weekdayISOM->format('l'), 
        $dowISO, $weekdayISOS->format('l')
    );
}

Results

STD:  0 = Sunday
ISOM: 7 = Sunday
ISOS: 7 = Sunday

STD:  1 = Monday
ISOM: 1 = Monday
ISOS: 1 = Monday

STD:  2 = Tuesday
ISOM: 2 = Tuesday
ISOS: 2 = Tuesday

STD:  3 = Wednesday
ISOM: 3 = Wednesday
ISOS: 3 = Wednesday

STD:  4 = Thursday
ISOM: 4 = Thursday
ISOS: 4 = Thursday

STD:  5 = Friday
ISOM: 5 = Friday
ISOS: 5 = Friday

STD:  6 = Saturday
ISOM: 6 = Saturday
ISOS: 6 = Saturday
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜