2013-09-24 99 views
-3

我有以下的日期时间称为“开始时间”字段在MySQL与“1899-12-30 12:17:52”转换日期时间在MySQL和PHP

我试图将其转换为一个时间在屏幕上为用户使用PHP使用

date('h:i:s A',strtotime($row_get_student_times['StartTime'])) 

但无论什么原因它总是返回下午4:00无论何时。任何线索为什么会发生这种情况?

+5

'1899'是老了的strtotime转换,你需要使用'DateTime' http://php.net/manual/en/class.datetime.php – cmorrissey

回答

0

您需要使用DateTime

$datetime = new DateTime($row_get_student_times['StartTime']); 
echo $datetime->format('h:i:s A'); 
+4

这已经得到解答[here。](http://stackoverflow.com/a/18665288/1438393),我相信问题是完全一样的。 –

1

这是因为你试图展示UINX时期(1970年)之前的日期。 试试这个:

$date = new DateTime($row_get_student_times['StartTime']); 
echo $date->format("h:i:s A"); 
0
<?php 
$dateTimeString = '1899-12-30 12:17:52'; 
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeString); 
echo $dateTime->format('H:i:s A');