php前端展示日期只要年月,Thinkphp 前端视图输出日期时间戳自动转换为时间格式化年月日...

需求:实现文章创建按年,月,日归类,并如图格式显示。

2018 年

11 月 ( 1 篇文章 )

24日: 你走了真好,不然总担心你要走 (130)

05 月 ( 1 篇文章 )

12日: 后来的我们 (90)

03 月 ( 2 篇文章 )

31日: 年少不懂《还珠3》,看懂已是而立年 (44)

12日: 要么孤独,要么庸俗 (60)

数据库 字段 article_createtime 格式为 timestamp

4de6f286eb374d6b81f4d9d925a2eb56-1.png

前端代码

{$article_res.article_createtime|strtotime|date="Y年",###}

  • {$article_res.article_createtime|strtotime|date="m月",###}

解释下:

strtotime()把字符串转化为整数时间

date(format, timestamp)把时间戳格式化为更易读的日期和时间

百度上搜索有的答案是:

{$article_res.article_createtime|date="y-m-d",###} ,经测试在timestamp格式下会得到一个异常,但是在int格式正常显示

4de6f286eb374d6b81f4d9d925a2eb56-2.png

有的习惯把日期用int格式保存,则 {$article_res.article_createtime|date="y-m-d",###} 正常显示,但 {$article_res.article_createtime|strtotime|date="y-m-d",###} 会得到1970-01-01.

需求中仅分别需要显示年,或月,或日,可以通过修改格式内容来获得需要的数据 date="y",### date="m",### date="d",###

date="y年",### :2019年

date="m月",###:11月

date="d日",###: 2日

总结

如果数据库是 timestamp 格式: {$article_res.article_createtime|strtotime|date="y-m-d",###}

如果数据库是 int 格式: {$article_res.article_createtime|date="y-m-d",###}

{$article_res.article_createtime|date="y-m-d",###}

{$article_res.article_createtime|strtotime|date="y-m-d",###}

timestamp

异常:A non well formed numeric value encountered

正确值

int

正确值

错误值:1970-01-01

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值