格式化'Y-m-d H:i:s'和strtotime。

时间:2022-05-11 21:28:37

I want to format $datetime = date('Y-m-d H:i:s'); to $time using strtotime($datetime) for display like 2016-01-20 11:16:31 But it does not work. How do I fix that?

我要格式化$datetime =日期('Y-m-d H: I:s');使用strtotime($datetime)来显示像2016-01-20 11:16:31的时间,但它不起作用。我怎么解决这个问题?

CODE

代码

$datetime = date('Y-m-d H:i:s'); 
$time = strtotime($datetime); 
echo $time; 
echo $datetime;

My code given output like this :

我的代码输出如下:

1453263391 // $time
2016-01-20 11:16:31 // $datetime

2 个解决方案

#1


4  

strtotime doesn't give you time like you expect. If you just need time remove the date placeholders

strtotime并没有像你想象的那样给你时间。如果您只是需要时间删除日期占位符。

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

strtotime is something else. It converts a properly formatted date time string into a UNIX timestamp which you seemingly don't need here.

strtotime是别的东西。它将正确格式化的日期时间字符串转换为UNIX时间戳,您在这里似乎不需要它。

#2


0  

strtotime() return unixtimestamp, so with unix timestamp you can custom your own date format with provided timestamp.

strtotime()返回unixtimestamp,因此使用unix时间戳,您可以使用提供的时间戳自定义自己的日期格式。

$datetime = date('Y-m-d H:i:s'); //or can be like $datetime = '2014-09-08 14:54:21';
$unix = strtotime($datetime);

$date = date('Y-m-d',$unix);
$time = date('H:i:s',$unix);
$hour = date('H',$unix);
$minute = date('i',$unix);
$month = date('m',$unix); //etc

#1


4  

strtotime doesn't give you time like you expect. If you just need time remove the date placeholders

strtotime并没有像你想象的那样给你时间。如果您只是需要时间删除日期占位符。

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

strtotime is something else. It converts a properly formatted date time string into a UNIX timestamp which you seemingly don't need here.

strtotime是别的东西。它将正确格式化的日期时间字符串转换为UNIX时间戳,您在这里似乎不需要它。

#2


0  

strtotime() return unixtimestamp, so with unix timestamp you can custom your own date format with provided timestamp.

strtotime()返回unixtimestamp,因此使用unix时间戳,您可以使用提供的时间戳自定义自己的日期格式。

$datetime = date('Y-m-d H:i:s'); //or can be like $datetime = '2014-09-08 14:54:21';
$unix = strtotime($datetime);

$date = date('Y-m-d',$unix);
$time = date('H:i:s',$unix);
$hour = date('H',$unix);
$minute = date('i',$unix);
$month = date('m',$unix); //etc