Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将JSON中的Mysql日期转换为毫秒_Php_Mysql_Json_Timestamp_Milliseconds - Fatal编程技术网

使用php将JSON中的Mysql日期转换为毫秒

使用php将JSON中的Mysql日期转换为毫秒,php,mysql,json,timestamp,milliseconds,Php,Mysql,Json,Timestamp,Milliseconds,我使用PHP对MySQL中的JSON进行了RESTfull编码,其中一列是date,在MySQL中是01/03/15 06:00,在JSON编码中是这样的 [["01/03/15 06:00","89"],["02/03/15 06:00","87"]] 如何将其转换为以下代码,其中日期以毫秒为时间戳 [["1420245000000","89"],["1422923400000","87"]] 用于JSON编码的PHP代码 private function productionhourly

我使用PHP对MySQL中的JSON进行了RESTfull编码,其中一列是date,在MySQL中是01/03/15 06:00,在JSON编码中是这样的

[["01/03/15 06:00","89"],["02/03/15 06:00","87"]]
如何将其转换为以下代码,其中日期以毫秒为时间戳

[["1420245000000","89"],["1422923400000","87"]]
用于JSON编码的PHP代码

private function productionhourlys(){   
        if($this->get_request_method() != "GET"){
            $this->response('',406);
        }
        $query="SELECT distinct  c.Date, c.RoA FROM productionhourlys c order by c.productionhourlyNumber desc";
        $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);

        if($r->num_rows > 0){
            $result[] = array_values([]);
            while($row = $r->fetch_row()) {
                $result[] = $row;
            }
            $this->response($this->json($result), 200); // send user details
        }
        $this->response('',204);    // If no records "No Content" status
    }

如果要在服务器端执行转换,请在while循环中执行转换,然后再将当前的$行分配给$result:

编辑:按操作要求将时间戳(以秒为单位)转换为毫秒

while($row = $r->fetch_row()) {
    $row[0] = strtotime($row[0]); // convert to unix timestamp (in seconds)
    $row[0] = 1000 * $row[0]; // convert seconds to milliseconds
    $result[] = $row;
}
我也不确定这一行的目的是什么:

$result[] = array_values([]);
如果只是创建一个新的空数组,可以执行以下操作:

$result = array();
请尝试以下示例:

<?php

$json = '[["01/03/15 06:00","89"],["02/03/15 06:00","87"]]';

$array = json_decode($json, true);

$array = array_map(function($n) {$date = new DateTime($n[0]); return array($date->format('U'), $n[1]);}, $array);

$json = json_encode($array);

echo $json;

这实际上并没有把时间转换成毫秒。@John Conde,谢谢,我没听清楚!更新了毫秒的答案。这实际上在32位系统上不起作用。您应该将timstamp视为一个字符串,并在末尾追加“000”。
[["1420282800","89"],["1422961200","87"]]