如何格式化时间戳

时间:2015-11-18 23:18:35

标签: php pdo timestamp

帮助我,我想把时间表放到这个格式时:小时:分钟:secondes

$services = $pdo ->prepare("SELECT * FROM `servicos` WHERE `DE` = :u");
$services->bindValue(":u", "1");
$services->execute();
echo $numServico = $services->rowCount(); // Contagem de serviços
$l = $services->fetchAll(PDO::FETCH_OBJ);
//$json = json_encode($l);
echo '{"data":[';
//echo $json;


foreach ($l as $listar) {



    $inicioHora = new DateTime("listar->REGISTRO");
    $inicioHora = $inicioHora->format('H:i:s');



}

但我收到了这个      致命错误:未捕获的异常'异常' with message' DateTime :: __ construct():无法解析位置0(l)处的时间字符串(listar-> REGISTRO):在数据库中找不到时区'在第26行

2 个答案:

答案 0 :(得分:2)

如果字段$ listar-> REGISTRO是时间戳,那么您只需要使用date()函数http://php.net/manual/en/function.date.php

$time = date('H:i:s', $listar->REGISTRO);

答案 1 :(得分:0)

替换它:

$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');

有了这个:

$inicioHora = date('H:i:s', strtotime($listar->REGISTRO));