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
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
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
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()
You could use mktime(), as others have suggested but strtotime() is a bit more flexible and user friendly.
date('l', strtotime('2014-07-26'));
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');