-1

Is there a way to use DateTime::CreateFromFormat to convert a 0-6 integer into the name of a day of the week?

I am creating a calendar for a script and would like to let users customize the way the days of the week display (Mon vs Monday) so using an array of the week day names is not desirable.

How can I use CreateFromFormat to do this?

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
  • I don't think `DateTime::CreateFromFormat` will convert an integer to a day of the week. However it should work with both "Mon" and "Monday". Here is another post with suggesting more alternate functions to do something similar: http://stackoverflow.com/questions/4961793/day-of-the-week-to-day-number-monday-1-tuesday-2 – John McMahon Apr 28 '15 at 02:53
  • 2
    You mean something [like this](https://eval.in/319675) using [this answer](http://stackoverflow.com/a/4742382/2812842)? – scrowler Apr 28 '15 at 03:07

2 Answers2

1

Step 1:

Get timestamp of first day of week +X (our integer) days.

// assuming monday = 0, sunday = 6
$day = 2; // our integer (wednesday)

// get timestamp of most recent monday +X days
$ts = (date('N') == 1) ? strtotime("today +$day days") : strtotime("last monday +$day days");

Step 2:

Print the short & long names of the weekday.

echo date('l', $ts); // Wednesday
echo date('D', $ts); // Wed
Sharn White
  • 624
  • 3
  • 15
0

Hope I understand your question.

$day = 0;
   $date = DateTime::createFromFormat('j-M-Y', $day . '-Feb-2010');
echo $date->format('l');

would output :

Sunday