从PHP中将日期存储在MySQL中的最佳做法 [英] Best practice for storing the date in MySQL from PHP

查看:186
本文介绍了从PHP中将日期存储在MySQL中的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用unix时间戳。



我喜欢它,因为它很容易比较,它很快,因为我把它存储为一个整数。而且,由于我使用PHP,我可以从unixtimestamp获取date()函数的日期/时间格式。



现在有人说最好使用DATETIME格式。但是除了更适合的名字,我看不到任何好处。



使用DATETIME确实更好吗?

谢谢。

解决方案

如果将日期作为Unix时间戳存储在数据库中,给自己一个沉重的举重。您必须将它们转换为要使用的格式,必须在日期范围之间进行计算,您必须构建查询以获取范围内的数据。这似乎是反直觉的 - 你的程序员时间最适用于解决实际问题?



似乎更好的做法是以适当的格式存储日期和时间MySQL可用,然后使用数据库函数创建所需数据的查询。你所浪费的所有的转换和溺水的时间与下午花了阅读(和理解)相比是一个很大的时间。 11.6 MySQL日期和时间函数


I've been using the unix timestamp all my life.

I like it because it's easy to compare, it's fast because I store it as an integer. And since I'm using PHP, I can get any date/time format with date() function from the unixtimestamp.

Now, some people are saying that it's best to use the DATETIME format. But besides the more suited name, I don't see any advantages.

Is it indeed better to use DATETIME, if so, what are the advantages?

Thanks.

解决方案

If you store dates as Unix timestamps in the database, you're giving yourself the heavy lifting. You have to convert them to the formats you want to use, you have to do the calculations between date ranges, you have to build the queries to get data in a range. This seems counter-intuitive- surely your "programmer time" is best spent solving real problems?

It seems much better practice to store dates and times in the proper format that MySQL has available, then use the database functions to create the queries for the data you want. The time you would waste doing all the convertions and mucking about is massive compared to the afternoon spent reading (and understanding) 11.6 MySQL Date and Time Functions

这篇关于从PHP中将日期存储在MySQL中的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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