1

I have a column that stores a 1-digit value for Day of Week:

0 = Sunday
1 = Monday
...
6 = Saturday

I tried PHP date('l', $row['dow']) and MySQL DATE_FORMAT(dow, '%w') but neither return the Day of Week word.

Is it possible in PHP or MySQL to do this or do I just need to create an array() var?

hakre
  • 193,403
  • 52
  • 435
  • 836
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • Thanks for the down vote. Just trying to learn something, that's all. Someday I'll be at your level and can assist someone with these types of sincere questions. – H. Ferrence Oct 22 '12 at 14:30
  • You mean the opposite of [day of the week to day number (Monday = 1, Tuesday = 2)](http://stackoverflow.com/questions/4961793/day-of-the-week-to-day-number-monday-1-tuesday-2)? Just asking, I'd say it's likely this has been asked before, so instead of asking it again, this needs some research on the site first. – hakre Oct 22 '12 at 14:31
  • Related: [want to print full Name of the day](http://stackoverflow.com/q/7063336/367456) – hakre Oct 22 '12 at 14:41

4 Answers4

2
$dow = date('l', strtotime("Sunday +{$row['dow']} days"));

Demo: http://codepad.viper-7.com/LzZDHf

Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
2

You need a full date/timestamp for any of the date formatting functions to work. Just a "1" or "2" doesn't mean anything to these functions (or at least not what you want it to mean). If you are storing a full timestamp, there's no real need to store the day of the week again separately. Otherwise, you'll need to translate those otherwise meaningless numbers to a word yourself.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • So the answer is you cannot do this in PHP using date() or MySQL using DATE_FORMAT() -- based on my question. You must setup your own array. Thanks for the answer @deceze ! – H. Ferrence Oct 22 '12 at 14:36
1

on date( 'l' ):

l (lowercase 'L') - A full textual representation of the day of the week - Sunday through Saturday

you should use date( 'w' ):

w - Numeric representation of the day of the week - 0 (for Sunday) through 6 (for Saturday)

Edit: ah, so you want the text representation from (only) the numer. Then a simple map would be enough:

$dayNames = array( 'Sunday', 'Monday', ..., 'Saturday' );
$day = $dayNames[$row['dow']];
hakre
  • 193,403
  • 52
  • 435
  • 836
pozs
  • 34,608
  • 5
  • 57
  • 63
0

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

here are all codes for php date format

From your database select your how date and with php use date('l', strtotime($date))

Svetoslav
  • 4,686
  • 2
  • 28
  • 43
  • Thanks @Svetlio -- but I have them already (keep them printed and pinned on my b-board next to my desk. My question is: can I take a single value of, say "2" and get PHP or MySQL to return "Tuesday" without creating an array()? I can create the array, no problem. Just preferring to do it a little simpler if possible...that's all. – H. Ferrence Oct 22 '12 at 14:23
  • I tried that earlier @Svetlio -- each value returns "Wednesday". My suspicion is is it defaulting to the UNIX start date of 1/1/1970. – H. Ferrence Oct 22 '12 at 14:25
  • do you have dates before 1970 ? – Svetoslav Oct 22 '12 at 14:27
  • No @Svetlio - -I have no dates. I simply have a column `day tinyint(1) UNSIGNED`. It contains a "0", or a "1", or a "2", or a "3", or a "4", or a "5", or a "6". That's it. – H. Ferrence Oct 22 '12 at 14:31
  • Then just create 1 php array eg.. $dates = array(0=>"Sunday", 1=>"Monday"....); and call it $dates[$row["dow"]] ; When you dont have Timeformat you cant use time functions .. – Svetoslav Oct 22 '12 at 14:39