开发者

php date conversion

How would i convert a format like:

13 hours and 4 mins ago

Into something i can use in php to further c开发者_如何转开发onvert?


try

strtotime('13 hours 4 mins ago', time())

http://php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/datetime.formats.php

http://www.php.net/manual/en/datetime.formats.relative.php


To get a unix timestamp fromt he current server time: $timestamp = strtotime("-13 hours 4 minutes");


Try the DateInterval class - http://www.php.net/manual/en/class.dateinterval.php - which you can then sub() from a regular DateTime object.


Do it the hard way:

$offset = (time() - ((60 * 60) * 13) + (4 * 60));

Working Example: http://codepad.org/25CJPL76

Explanation:

(60 * 60) is 1 hour, then * 13 to make 13 hours, plus (4 * 60) for the 4 minutes, minus the current time.

What i usually do is create several constants to define the values of specific time values like so:

define("NOW",time());
define("ONE_MIN", 60);
define("ONE_HOUR",ONE_MIN * 60);
define("ONE_DAY", ONE_HOUR * 24);
define("ONE_YEAR",ONE_DAY * 365);

and then do something like:

$offset = (NOW - ((ONE_HOUR * 13) + (ONCE_MIN * 4)));

in regards to actually parsing the string, you should look at a javascript function and convert to PHP, the source is available from PHP.JS:

http://phpjs.org/functions/strtotime:554


Use strtotime(), and perhaps date() later, e.g.:

$thetime = strtotime('13 hours 4 minutes ago');
$thedate = date('Y-m-d', $thetime);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜