Using the following PHP date formats, you can get today's day relative to a week:
Character | Description |
---|---|
D |
A three-letter textual representation of a day (Mon through Sun ). |
l |
A full textual representation of the day of the week (Sunday through Saturday ). |
N |
ISO-8601 numeric representation of the day of the week (where 1 denotes Monday and 7 denotes Sunday). |
w |
Numeric representation of the day of the week (where 0 denotes Sunday and 6 denotes Saturday). |
For example:
$dt = new DateTime(); echo $dt->format('D'); // output: 'Mon' - 'Sun' echo $dt->format('l'); // output: 'Monday' - 'Sunday' echo $dt->format('N'); // output: 1 - 7 echo $dt->format('w'); // output: 0 - 6
Or, alternatively, you can achieve the same using the date()
function, like so:
echo date('D'); // output: 'Mon' - 'Sun' echo date('l'); // output: 'Monday' - 'Sunday' echo date('N'); // output: 1 - 7 echo date('w'); // output: 0 - 6
To include the timezone, you could do the following:
$tz = new DateTimeZone('Europe/Berlin'); $dt = new DateTime('now', $tz); echo $dt->format('D'); // output: 'Mon' - 'Sun' echo $dt->format('l'); // output: 'Monday' - 'Sunday' echo $dt->format('N'); // output: 1 - 7 echo $dt->format('w'); // output: 0 - 6
You can achieve the same with the date()
function by setting the timezone using date_default_timezone_set()
. However, this isn't the ideal solution as it would set the default timezone globally (i.e. it will be used by all date/time functions). To avoid that, you should use the DateTime
object instead (as shown above).
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.