将数据类型设置为Date&时间只在PHP中

时间:2014-05-20 05:56:08

标签: php mysql

我有一个mysql数据库,每个都有一个时间和日期列,有没有办法将每个结果配置为仅在返回时给我一个日期数据类型,我必须在myphp中手动切换到日期只和时间切换。我已经检查了php手册,但我似乎无法在线找到答案。

    Print "<td><center>".$row['Transdate']."</center></td> ";
    Print "<td><center>".$row['Transtime']."</center></td> ";   

这两行是我要格式化的,你能举个例子来解释一下吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

这个怎么样:

$originalDatetime = "2038-01-19 03:14:07";

list($datePortion, $timePortion) = explode(" ", $originalDatetime);

或者在MySQL查询中:

SELECT DATE(myDatetimeColumn) as MyDate, TIME(myDatetimeColumn) as MyTime FROM myTable;

答案 1 :(得分:1)

您的数据库中有datetime列。例如,对于ID 154,有一个名为Date的列,其值为2014-05-20 09:15。所以你在变量$row['Date']中得到它:

$timestamp = strtotime($row['Date']);
$Transdate = date('Y-m-d', $timestamp );
$Transtime = date('H:i', $timestamp );

echo $Transdate."<br/>".$Transtime

将打印:

2014年5月20日

九点15