Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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存储从mysql检索格式化日期时间_Php_Mysql - Fatal编程技术网

用php存储从mysql检索格式化日期时间

用php存储从mysql检索格式化日期时间,php,mysql,Php,Mysql,首先很抱歉,如果之前有人问过这个问题,我几乎可以肯定,但是我找不到一个帖子完全回答我的问题,关于我希望获得的东西,有很多相互矛盾的建议 下面是我的用户流,用于使用api插入数据 这是我的桌子 CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(250) NOT NULL, `releaseDate` TIMESTAMP DEFAULT CURR

首先很抱歉,如果之前有人问过这个问题,我几乎可以肯定,但是我找不到一个帖子完全回答我的问题,关于我希望获得的东西,有很多相互矛盾的建议

下面是我的用户流,用于使用api插入数据

这是我的桌子

CREATE TABLE `test` (
         `id` int(11) NOT NULL AUTO_INCREMENT,
         `title` varchar(250) NOT NULL,
         `releaseDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
         `createdDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
         PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
将数据发布到api

# POST /v1/tests
? title=test
这将使用createdDate当前时间戳和releaseDate当前时间戳创建我的记录

现在,我想通过GETAPI调用检索数据,调用的日期格式为ISO8601

// My test model just retrieves data for a row
function test_model_test($id){

    $query = $this->db->get_where('test', array('id' => $id)0);
    return ($query->num_rows() > 0) ? $query->row() : false;

}

 // Test api call to retrieve the data I need the date in ISO8601 format
function test_get(){

    $data = $this->test_model->test($this->get('id'));

    $response = array(
        'id' => $data->id,
        'title' => $data->title,
        'releaseDate' => $data->releaseDate->format(\DateTime::ISO8601)
    }

    $this->response([
        'status' => TRUE,
        'message' => 'Successful',
        'data' => $response
    ], REST_Controller::HTTP_OK);

}
当我运行这个时,我得到了错误

Call to a member function format() on string /
我是否需要将日期从stings转换为date,然后格式化

我可以使用mysql以如下的currect格式返回数据

$query = $this->db->query('SELECT *, 
                DATE_FORMAT(releaseDate, "%Y-%m-%dT%TZ") AS releaseDateFormatted, 
                DATE_FORMAT(createdDate, "%Y-%m-%dT%TZ") AS createdDateFormatted,  
                FROM test 
                WHERE id = ' . $id . ' 
            ');
我不确定最好的方法可能有很多,我想如果我保持mysql干净,这将允许我在php中更改其他格式的格式,如果需要的话

非常感谢您对最佳实践等方面的建议

谢谢

更新此工程

$releaseDate = new DateTime($value->releaseDate);
$releaseDate->format(\DateTime::ISO8601);

你现在做的很好。嗨@FrankerZ哪边?我建议使用两种不同的方式格式化日期一种是MySQL,另一种是PHP。我更喜欢PHP方式,但我不确定格式化返回日期的最佳方式是什么?谢谢你应该用PHP进行格式化-当然,你必须首先解析日期字符串,将其转换为日期对象,然后再应用格式化(这不可避免地会返回一个字符串)。谢谢,我已经用want更新了我的答案;)