开发者

PHP: How to get Day using Y-m-d?

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


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


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

date('l', strtotime('2014-07-26')); 


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


a very easy way is to use strtotime:

date("l",strtotime("2014-07-26"));


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 


The second arg needs to be a timestamp.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜