tags:

views:

142

answers:

8

I am trying to get Day(Monday, Tuesday, Wednesday,...) using "Y-m-d" date format but it is not giving me exact day.

For example:

date("l","2014-07-26"); // Output: Thursday

But output should be: Saturday

Thanks

A: 

You've got to use mktime to convert the date to a timestamp first

date("l", mktime(0, 0, 0, 7, 26, 2014));

date mktime

maggie
+1  A: 

php date function accepts a format and a timestamp. Using "2014-07-26" as a timestamp is not correct. You should be using unix epoch timestamp.

TO do this you could for example do:

$t = "2014-07-26"; 
$t = explode("-", $t); //not $t = array("2014", "07", "26"); 
echo date("1", mktime(0,0,0,$t[1], $t[2], $t[0]); //using mktime with 
                                                  //hour, min, sec, month, day, year 
Chris
+1  A: 

The second arg needs to be a timestamp.

Darragh
+13  A: 
date("l",strtotime("2014-07-26")); //Output: Saturday
Awan
strtotime() can get it wrong if you're using UK date format and the day is less than 12
TonyVipros
need to set `date_default_timezone_set('ABC/xyz');` ?
Mithun P
@TonyVipros At first I thought "Good point!" then realised that doesn't apply here as with YYYY-MM-DD there is no ambiguity (there is no such thing as YYYY-DD-MM anywhere). I should probably note just for clarification that DD-MM-YYYY is not really a "UK thing" is a "world thing" - only a handful of countries in the world use the (very silly) US method 8)
Iain Collins
+3  A: 

date() takes a unix timestamp as it's second parameter, not a string representation of a date. If you want to do that, you should convert to a unixtimestamp first using strtotime()

Mike Sherov
+2  A: 

a very easy way is to use strtotime:

date("l",strtotime("2014-07-26"));
oezi
+3  A: 

You could use mktime(), as others have suggested but strtotime() is a bit more flexible and user friendly.

date('l', strtotime('2014-07-26')); 
Iain Collins
Actually Gordon's suggestion of using the DateTime class is probably a better idea (unless it's really more appropriate to stick to a one liner).
Iain Collins
+8  A: 

Or with DateTime object

$dt = new DateTime('2014-07-26');
echo $dt->format('l'); // Saturday

or with the procedural aliases

echo date_format(date_create('2014-07-26'), 'l');
Gordon
Oh that's much better than my (and everyone elses) strtotime() solution. Shame on me for not suggesting the DateTime() class.
Iain Collins
@Iain it's an alternative. Nothing more, nothing less.
Gordon
In the case of PHP I actually think it's worth advocating newer OO interfaces because the naming conventions and argument order of older built-in methods is so inconsistent. Not that I consistently practice what I'm preaching here. *ahem*
Iain Collins
@Iain Collins: For what it's worth, the DateTime constructor actually uses strtotime. When you're just doing date formatting, DateTime can be a bit overkill, even though it's totally awesome. Remember: Always try to use the most simple solution that might work.
Charles
@Charles I agree that strtotime() works in this example; but disagree the 'most simple solution that might work' is what PHP developers, specifically, need to be doing more of. There are some very compelling reasons to get into the habit of using DateTime class (timezone handling, support for wider date ranges, exception handling and extension, anytime you need to do additional date+time manipulation). Bit of a general OOP vs procedural debate though.
Iain Collins
@Iain, it's also a development methodology debate. My team latched on to "the most simple thing that could possibly work" as an argument against our old lead dev's tendency towards over-engineering entire systems. We've since extended the semi-philosophy to a mantra to make sure we don't fall into the same trap. DateTime is indeed awesome, and we're using it throughout our new projects exclusively. But sometimes, sometimes you just need something simple and stupid. :)
Charles