Laravel Mysql语句中时间戳/日期格式相互转换
Song •
4276 次浏览 •
0个评论 •
2020年06月03日
一、Mysql中时间戳和日期的转换
熟悉Mysql的都知道我们可以使用UNIX_TIMESTAMP和FROM_UNIXTIME进行时间戳和日期的转换
select UNIX_TIMESTAMP('2020-06-01 12:25:00');
结果:1590985500
案例:
select id,username,UNIX_TIMESTAMP(updated_at) AS updated_date from users;
2.时间戳转日期:
select FROM_UNIXTIME(1545711900);
结果:2020-06-01 12:25:00
案例:
select id,username,FROM_UNIXTIME(created_at,'%Y-%m-%d %H:%i:%s') from users;
二、Laravel中数据库时间戳和日期格式的转换
我们可以使用原始表达式在Model模型进行在日期和时间之间转化:
# 时间戳转日期
DB::raw("date_format(from_unixtime(created_at),'%Y-%m-%d %H:%i:%s') as created_date")
# 日期转时间戳
DB::raw("from_timestamp(updated_at) as updated_date")
然后我们结合真实场景使用:
DB::table('users')->select(DB::raw("date_format(from_unixtime(created_at),'%Y-%m-%d %H:%i:%s') as created_date"),"created_at","province")->get();
-
laravel中distinct()的使用方法与去重 2017-09-11 -
Laravel将view缓存为静态html,laravel页面静态缓存 2021-10-09 -
[ laravel爬虫实战--基础篇 ] guzzle描述与安装 2017-11-01 -
[ 配置教程 ] 在ubuntu16.04中部署LNMP环境(php7+maridb且开启maridb远程以及nginx多域名访问 )并配置laravel环境 2017-07-18 -
微信公众号回复菜单点击回复文本 2025-10-24
热门文章
-
微信公众号回复菜单点击回复文本 2025-10-24 -
laravel+easywechat6出现No component_verify_ticket found以及修改缓存为redis 2025-09-09 -
nginx 服务器如何查看当前访问的域名 2025-06-10 -
ubuntu+nginx当服务器异常时微信/企业微信/抖音数据重复回调导致服务器崩溃怎么解决? 2025-05-30 -
Jquery使用xlsx实现批量导入Excel数据 2025-05-12

更多相关好文