开发者

datetime to timestamp [duplicate]

开发者 https://www.devze.com 2023-03-10 08:00 出处:网络
This question already has answers here: 开发者_如何转开发 Closed 11 years ago. Possible Duplicates:
This question already has answers here: 开发者_如何转开发 Closed 11 years ago.

Possible Duplicates:

Inverse date function? - not strtotime

Is it possible to get UNIX time from such date 2011-02-27 02:04:46?

hello,

we have this function to convert a timestamp to datetime:

$datetime = date('Y-m-d H:i:s', $timestamp);

is there a function to do the opposite?

datetime to timestamp.

Thanks


$timestamp = strtotime($datetime);

Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.

Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.

EDIT:

A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:

$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');

See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.


$timestamp = strtotime('12-04-2010');
0

精彩评论

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