开发者

Unix time to real time

Not sure whats up with the code

$date = strtotime("%b %d, %Y", $datedata);
    $time = strtotime("%I:%M:%S %p", $datedata);

The time i gets from the DB is 1298747601 and is the $datedata

I have date_default_timezone_set('America/Los_Angeles'); at the top of the scrip开发者_开发技巧t.


Use strftime() or date(), not strtotime(). Strtotime is meant to format textual dates, not timestamps.

It's better to use the DATETIME datatype in your database instead of an int containing a timestamp. Using a DATETIME means easier calculating with dates and you can format the date directly in your query.


strtotime() is for converting a string to a time like, $timestamp = strtotime("1:33 PM EST Next Friday");.

You want date(), which takes a format string and a timestamp to create a formatted date time string, and uses the current time (from time()) if no timestamp is passed, like date("h:i:s A T, M jS, Y", $timestamp) which would output something like "1:33:00 PM EST, March 4th, 2011". Also, note that if you want to do words in the format, like "23rd of January", and the letters in the words are also date formatting characters, in this case the 'o' in 'of' is the ISO-8601 year number formatting option, you must escape it with a \ slash like "jS \of F". As the f is not a formatting option, it does not need to be escaped, but if it did, then it would be "\o\f"

There's also a DateTime object that's built into more recent PHP versions that you can look into.

I disagree with Ray that it is better to store a DateTime datatype in SQL rather than an integer timestamp. They're about the same. You can still search for date ranges in the DB simply by finding numbers greater than X timestamp and less than Y timestamp, and, as a plus, everything that you get from the DB is already in a timestamp format that can be used easily by PHP without having to convert it to a proper timestamp.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜