H. Ferrence
H. Ferrence

Reputation: 8106

PHP or MySQL Date Format for Day of Week

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?

Upvotes: 1

Views: 2204

Answers (4)

pozs
pozs

Reputation: 36214

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']];

Upvotes: 1

deceze
deceze

Reputation: 522016

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.

Upvotes: 2

Ben Swinburne
Ben Swinburne

Reputation: 26467

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

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

Upvotes: 2

Svetoslav
Svetoslav

Reputation: 4676

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))

Upvotes: 0

Related Questions