开发者

convert unix timestamp php

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)开发者_C百科;
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?


You can use the strtotime and date function:

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


Use date.

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


here you go

print date("YmdHis",unixtimestamp);


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.


Use the right format :

date('YmdHis');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜