数据库一般来说,存的时间为uninx时间戳,根据需求,可能会将查询出来的日期进行格式化
1、如果结果集只有一条,则可在查询后,使用date函数转化即可
$time = date("Y-m-d H:i:s",$time);
2、如果有多条结果集,不建议遍历结果集,一一格式化时间,可直接在sql中使用from_unixtime(),该函数是mysql中的函数;
select from_unixtime(create_time,"%Y-%m-%d") as time from student
3、如果在Yii中,使用AR查询,不直接写sql,则在使用的时候会报错
Student::find()
->select("id,name,from_unixtime(create_time,"%Y-%m-%d") create_time")
->asArray()
->all();
这样写会报错,因为AR中,会在查询时,以逗号为分割,默认给每一个元素加上“上引号”,如果使用函数,会给整体再加上“上引号”,出现转义错误,所以使用如下方法:
Student::find()
->select(new Expression("id,name,from_unixtime(create_time,"%Y-%m-%d") create_time"))
->asArray()
->all();
使用new Expression()处理一下就好了
备注:
使用 FROM_UNIXTIME函数ÿ