Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Number to Day of Week

Tags:

date

php

time

I need to convert a number to a date ie

1 -> Sunday
2 -> Monday

I have the following question which aids with the opposite: day of the week to day number (Monday = 1, Tuesday = 2)

Once I am able to do this I could for example loop through and echo the day ie:

while($i < 7)
{
    echo date("N",strtotime(1));
}

Everything I am searching for seems to be people asking to convert a day to a number. Can someone please point me in the right direction

like image 815
The Humble Rat Avatar asked Mar 17 '23 08:03

The Humble Rat


1 Answers

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')];
like image 183
John Conde Avatar answered Mar 18 '23 21:03

John Conde