5

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

Community
  • 1
  • 1
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73

2 Answers2

9

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')];
Rizier123
  • 58,877
  • 16
  • 101
  • 156
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Many thanks for your response. I have seen similar to this and was hoping I could avoid having to create an array as I will likely use this multiple times, so i can create a method to return this. Out of interest, there was another answer that has since been deleted. `$dow_text = date('D', strtotime("Sunday +{$dow_numeric} days"));` I wonder if you had any opinion on this method, I ask purely because it's a shorter way, albeit by a few lines. – The Humble Rat Apr 21 '15 at 13:16
  • Actually I have found the following link: http://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/. This describes both methods and states the method shown above in your answer is much quicker as apparently `date` and `strtotime` functions can be quite slow. – The Humble Rat Apr 21 '15 at 13:22
  • It cause problem if day 1 is not a Sunday... try that... :) – Eko Junaidi Salam Apr 21 '15 at 13:22
0

You just need to create an Array to do that, Please check this :

$dayOfWeek = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
echo $dayOfWeek[2]."<br />";
echo $dayOfWeek[8%7];
echo $dayOfWeek[date('N')];

Why I write an array like that ? index 0 => "Sunday" and 1 => "Monday" .... ? Because if it's a day you must todo that, 1 week is 7 day so the rest you can use "Modulo / %(in php)" to convert that.

For example : If date 1 March 2015 is Sunday so the day 8 must be Monday because 8 mod 7 is 1 or 8%7 (in php) is 1. So to convert that you just do this $dayOfWeek[8%7] to get the dayOfWeek. :)

For another conditional case you need your logic to do that. If day 1 May 2015 is Friday. Just keep in mind to check the year it was to know the February day count is 28 or 29.

How to check ? please try this :

$year = 2015;
$feb = (($year%100)and !($year%4)or !($year%400))+28;
echo $feb;

Day in year should like this :

$month= array(0,31,$feb,31,30,31,30,31,31,30,31,30,31);

0=0, 1=January,2=February,....

Hope this help you out.. :)

Eko Junaidi Salam
  • 1,663
  • 1
  • 18
  • 26