解析时间戳 - 在MySQL或PHP中执行? [英] Parsing timestamps - do it in MySQL or in PHP?

查看:136
本文介绍了解析时间戳 - 在MySQL或PHP中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一张表格,里面有一个 timestamp 列,你想将这个列解析成两个数组 - $ date $ time



您个人:



a)像这样查询 DATE(时间戳),TIME(时间戳),甚至可能远远超过 HOUR时间戳),MINUTE(时间戳记



b)获取时间戳列并解析如果需要,在PHP中循环



我觉得(a)更容易...但我知道那个我不知道什么。感觉有点调皮,让我的查询在同一列中输出了两到三次...



这是最好的做法吗?

解决方案

(a)如果您的代码更容易,我是一个不需要编写不需要的额外代码的大粉丝,我只想在必要时进行优化。给我拉整个日期,然后解析似乎是过早的优化。



永远记住,sql服务器在他们中有很多智能优化查询,所以你不必。



所以去a),如果你发现它是狗慢或引起问题,然后去b)。我怀疑a)会做所有你想要的,你永远不会再考虑它。


Let's say you've got a table with a timestamp column, and you want to parse that column into two arrays - $date and $time.

Do you, personally:

a) query like this DATE(timestamp), TIME(timestamp) , or perhaps even going as far as HOUR(timestamp), MINUTE(timestamp

b) grab the timestamp column and parse it out as needed with a loop in PHP

I feel like (a) is easier... but I know that I don't know anything. And it feels a little naughty to make my query hit the same column 2 or 3 times for output...

Is there a best-practice for this?

解决方案

(a) is probably fine, if it is easier for your code base. I am a big fan of not having to write extra code that is not needed and I love only optimizing when necessary. To me pulling the whole date and then parsing seems like premature optimization.

Always remember that sql servers have a whole lot of smarts in them for optimizing queries so you don't have to.

So go with a), if you find it is dog slow or cause problems, then go to b). I suspect that a) will do all you want and you will never think about it again.

这篇关于解析时间戳 - 在MySQL或PHP中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆