convert unix timestamp php

我的未来我决定 提交于 2019-12-01 06:09:30

问题


I have a database that stores the time for me. I insert it from PHP using

date( 'Y-m-d H:i:s');

I then use this function to convert it to a unix timestamp in PHP

function convert_datetime($str) 
{
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);

$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

return $timestamp;
}

What I now need to do is convert this timestamp to the date and time in this format: yyyymmddhhmmss (without any hyphens, dashes, colons, etc).

Has anyone any ideas?


回答1:


You can use the strtotime and date function:

echo date('Ymdhis', strtotime($date));



回答2:


Use date.

$date = date("YmdHis", $timestamp);



回答3:


here you go

print date("YmdHis",unixtimestamp);



回答4:


If you're using mysql, it has a DATE_FORMAT function. You can call it like

SELECT DATE_FORMAT(`datefield`, "%Y%m%d%H%i%s") AS `date_formatted` FROM table;

So you get an already formatted date from your database.




回答5:


Use the right format :

date('YmdHis');


来源:https://stackoverflow.com/questions/3707506/convert-unix-timestamp-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!