Dates in PHP and MySQL

Source: Internet
Author: User
Tags php and mysql

I see a lot of people on forums and on my training courses asking about the best way (or any way) to manage dates stored in a MySQL database and used in PHP. three options follow, but first the problem.
PHP uses unix timestamps for all its date functionality. it has methods to convert these timestamps into pretty much any text format you coshould want but internally it uses the timestamp format. A timestamp is simply an integer. specifically, it's the number of seconds that have elapsed since midnight on January 1 st 1970 (greenwich mean time ).
MySQL has three date types for use in columns. these are DATETIME, DATE, and TIMESTAMP. DATETIME columns stores date and time as a string in the form YYYY-MM-DD HH: MM: SS (e.g. 13:43:15 ). DATE columns use the date part of this format-YYYY-MM-DD (e.g. 2006-12-25 ). TIMESTAMP columns, despite their name, are nothing like the unix timestamps used in PHP. a timestamp column is simply a DATETIME column that automatically updates to the current time every time the contents of that record are altered. (That's a simplification but broadly true and the details are not important here ). in particle, since version 4.1 of MySQL the TIMESTAMP format is exactly the same as the DATETIME format.
So the problem is how to work with these two very different date formats-the PHP timestamp integer and the MySQL DATETIME string. There are three common solutions...
One common solution is to store the dates in DATETIME fields and use PHPs date () and strtotime () functions to convert between PHP timestamps and MySQL DATETIMEs. The methods wocould be used as follows-
$ Mysqldate = date ('Y-m-d H: I: s', $ phpdate );
$ Phpdate = strtotime ($ mysqldate );
Our second option is to let MySQL do the work. mySQL has functions we can use to convert the data at the point where we access the database. UNIX_TIMESTAMP will convert from DATETIME to PHP timestamp andFROM_UNIXTIME will convert from PHP timestamp to DATETIME. the methods are used within the SQL query. so we insert and update dates using queries like this-
$ Query = "UPDATE table SET
Datetimefield = FROM_UNIXTIME ($ phpdate)
WHERE ...";
$ Query = "SELECT UNIX_TIMESTAMP (datetimefield)
FROM table WHERE ...";
Our last option is simply to use the PHP timestamp format everywhere. since a PHP timestamp is a signed integer, use an integer field in MySQL to store the timestamp in. this way there's no conversion and we can just move PHP timestamps into and out of the database without any issues at all.
Be aware, however, that by using an integer field to store your dates you lose a lot of functionality within MySQL because MySQL doesn't know that your dates are dates. you can still sort records on your date fields since php timestamps increase regularly over time, but if you want to use any of MySQL's date and time functions on the data then you'll need to use FROM_UNIXTIME to get a MySQL DATETIME for the function to work on.
However, if you're just using the database to store the date information and any manipulation of it will take place in PHP then there's no problems. www.2cto.com
So finally we come to the choice of which to use. for me, if you don't need to manipulate the dates within MySQL then there's no contest and the last option is the best. it's simple to use and is the most efficient in terms of storage space in the data table and speed of execution when reading and writing the data.
However, some queries will be more complicated because your date is not in a date field (e.g. select all users who's birthday is today) and you may lose out in the long run. if this is the case it may be better to use either option 1 or 2. which of these you use depends on whether you 'd rather place the work on MySQL or PHP. I tend to use option 2 but there's no right or wrong answer-take your pick.
So to summarise, for those who 've skipped straight to the last paragraph, most of the time I use option 3 but occasionally I use option 2 because I need MySQL to know the field contains a date.

Author: chenjie3392593

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.