开发者

strftime in dates?

When I had UNIX timestamps, I'd write:

strftime("%A", $date)

But now I have datestamps, like "2011-08-0开发者_如何转开发2"

How can I make it output the weekday name, e.g "Sunday"?


You can convert the date stamp to a timestamp using the function strtotime. Once you have the timestamp, you can just use the function date to show the date in the desired format.


First use the strtotime function to convert the '2011-08-02' to a UNIX timestamp, and then proceed as you usually would

For example, the following are equivalent:

$date = 1312243200; // A unix timestamp
$date = strtotime('2011-08-02'); // The date that it represents

You can then do whatever you would usually do with the result

The strtotime() function is fairly forgiving in what date formats it accepts and even accepts values such as '8pm tomorrow' or 'last Monday' - See http://www.php.net/strtotime


Use date('l'); Add more properties like so: date('l d-m-Y');.

More info here


(2nd August 2011 was a Tuesday, not a Sunday.)


<?php
echo strftime("%A", strtotime("2011-08-02"));
// Output: "Tuesday"
?>
  • Live demo
  • strtotime documentation


date('l',strtotime('2011-08-02'));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜