如何使用PHP从日期时间字符串中分隔日期和时间?

时间:2011-08-04 13:15:16

标签: php datetime

如何转换此字符串

$parent = "2011-08-04 15:00:01";

将它分成两个字符串:

$child1= "2011-08-04";
$child2 = "12:00:01";

然后转换

$child1 to this format 8.4.2011 

$child2 to this format 15:00

3 个答案:

答案 0 :(得分:7)

$parent = '2011-08-04 15:00:01';

$timestamp = strtotime($parent);

$child1 = date('n.j.Y', $timestamp); // d.m.YYYY
$child2 = date('H:i', $timestamp); // HH:ss

答案 1 :(得分:6)

$time = new DateTime("2011-08-04 15:00:01");
$date = $time->format('n.j.Y');
$time = $time->format('H:i');

答案 2 :(得分:0)

date('Y-m-d', strtotime( '2015-04-16 15:00:01' ) );

这将为您提供正确的日期格式,后跟mysql 即2015-04-16 03:54:17