到目前为止的strtotime返回false

时间:2016-11-10 18:01:16

标签: php datetime

我有这组unix时间戳:

  1. 1473804000 1471831200
  2. 我无法以此格式('Y-m-d H:i:s')为每个unix时间戳分配日期。

    我的代码:

    $eventhour = '1473804000';
    $client = array(
                    'pt' => $pt,
                    'eventhour' => date('Y-m-d H:i:s', $eventhour),
                    'activepower' => $avg['system.avg(activepower)']
                );
    

    eventhour键为空,我猜函数date返回false。 unix时间戳是有效的,为什么它返回false?

    我的循环代码

    foreach ($result as $row):
            $pt = $row['pt'];
    
            $statement = new \Cassandra\SimpleStatement("select avg(activepower) FROM datavalue_test_by_hour WHERE pt= :pt AND eventhour = :eventhour;");
            $options = new \Cassandra\ExecutionOptions(
                array('arguments' => array('pt' => $pt, 'eventhour' => $row['eventhour'])))
            ;
            $data = $this->cassandra->executeWithOptions($statement, $options);
            $eventhour = $row['eventhour'];
            echo $eventhour . PHP_EOL;
            $avg = $data->first();
            $client = array(
                'pt' => $pt,
                'eventhour' => date('Y-m-d H:i:s', $eventhour),
                'activepower' => $avg['system.avg(activepower)']
            );
            print_r($client);
            //fputcsv($fp, $client);
        endforeach;
    

    我的print_r():

    1462554000
    Array
    (
        [pt] => AFH0AEFB0BFDEI
        [eventhour] => 
        [activepower] => 0.02475
    )
    1474887600
    Array
    (
        [pt] => A0A0AEAF0DI0H0
        [eventhour] => 
        [activepower] => 0.1115
    )
    1475244000
    Array
    (
        [pt] => AFH0ADE0AB0GDI
        [eventhour] => 
        [activepower] => 0.0905
    )
    1473008400
    Array
    (
        [pt] => A0A0AEAF0GDFHB
        [eventhour] => 
        [activepower] => 0.014
    )
    1470693600
    Array
    (
        [pt] => AFH0AEE00AF0FG
        [eventhour] => 
        [activepower] => 0.051
    )
    1452200400
    Array
    (
        [pt] => AFH0AEE00BACIC
        [eventhour] => 
        [activepower] => 0.152
    )
    1463637600
    Array
    (
        [pt] => AFH0ACFB00CD0F
        [eventhour] => 
        [activepower] => 0.041
    )
    

1 个答案:

答案 0 :(得分:0)

我通过将返回的$row['eventhour']从数据库转换为 INT 来解决了这个问题。

$eventhour = (int) $row['eventhour'];

现在正在工作。