PHP Day of Week Numeric to Day of Week Text

PHP day of week numeric to day of week text

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.

day of the week to day number (Monday = 1, Tuesday = 2)

$day_of_week = date('N', strtotime('Monday'));

PHP day of week numeric to day of week text not English

nl_NL is a typical Unix style locale name. On Windows, locales follow a different format. You want "nld", "holland", or "netherlands".

Additionally, date() is not locale-aware. You probably want strftime().

Convert Number to Day of Week

Just put the days in an array using the day of the week as the key. Then use date('N') to get the day of the week and use that as they key to access that array value.

$days = [
1 => 'Sunday',
2 => 'Monday',
3 => 'Tuesday',
4 => 'Wednesday',
5 => 'Thursday',
6 => 'Friday',
7 => 'Saturday'
];

echo $days[date('N')];

Getting Day name from day of week

You could use the date function, with the format l:

l (lowercase 'L')

A full textual representation of the day of the week

http://php.net/manual/en/function.date.php

and use the fact that Sunday is 0:

echo date('l', strtotime("Sunday +{$day_number} days"));

Or as a function:

function getDayNameFromDayNumber($day_number) {
return date('l', strtotime("Sunday +{$day_number} days"));
}

How to find the date of a day of the week from a date using PHP?

I think this is what you want.

$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));


Related Topics



Leave a reply



Submit