php 转换unix时间戳php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3707506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert unix timestamp php
提问by 109221793
I have a database that stores the time for me. I insert it from PHP using
我有一个为我存储时间的数据库。我使用 PHP 从 PHP 插入它
date( 'Y-m-d H:i:s');
I then use this function to convert it to a unix timestamp in PHP
然后我使用此函数将其转换为 PHP 中的 unix 时间戳
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).
我现在需要做的是将此时间戳转换为以下格式的日期和时间:yyyymmddhhmmss(没有任何连字符、破折号、冒号等)。
Has anyone any ideas?
有没有人有任何想法?
回答by Sarfraz
You can use the strtotime
and date
function:
您可以使用strtotime
anddate
函数:
echo date('Ymdhis', strtotime($date));
回答by Grumpy
here you go
干得好
print date("YmdHis",unixtimestamp);
回答by Maerlyn
If you're using mysql, it has a DATE_FORMAT
function. You can call it like
如果您使用的是 mysql,它有一个DATE_FORMAT
功能。你可以称之为
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.
所以你从你的数据库中得到一个已经格式化的日期。
回答by Toto
Use the right format :
使用正确的格式:
date('YmdHis');