Php & mysql date operation note

Source: Internet
Author: User
In many mature php frameworks, when the database storage time is of the int type rather than the datetime type, the int efficiency is significantly higher when the time is compared and queried. Xiang Wen see http://www.bitsCN.com/article/29767.htm
However, when you are working on a project or directly viewing data in the database, it is obvious that the int value is big at first glance. for example, we want
To view the registration time of a user:
Select reg_time from t_xx_users where user_id = 1;
At this time, the return value is an int value and the specific time cannot be intuitively viewed. Therefore, the conversion of datetime and int is involved,
In addition, the date and time of php also involve the corresponding conversion. This document briefly summarizes the following:
(1) php
Int value:
Time (): returns the number of seconds from the Unix epoch (GMT 00:00:00, January 1, January 1, 1970) to the current time.
We can use strtotime () to obtain the number of seconds from January 1-20, 1970 to 2012: strtotime ('2017-2-10 ');
Date value:
String date (string format [, int timestamp])
For example, the current time can be returned directly by date (). of course, we can specify its format: for example, date ('Y-m-D ', strtotime ('2014-2-10 '));
Time operation:
Date ('Y-m-d h: I: S', strtotime ('+ 1 week '));
Date ('Y-m-d h: I: S', strtotime ('+ 5 hours '));
Date ('Y-m-d h: I: S', strtotime ('next Monday ));
Date ('Y-m-d h: I: S', strtotime ('last Sunday '));
Date ('Y-m-d h: I: S', strtotime ('+ 1 Day', 12313223 ));!! For details, see int strtotime (string time [, int now]).

(2) mysql:
Int-> datetime
Select from_unixtime (int_time) from table;
Datetime-> int;
Select unix_timestamp (date_time) from table;
Time operation:
Select dayofweek ('1970-2-2 '); returns the day of the week.
Select dayofmonth ('1970-2-2 '); returns the day from January 1, 2012.
Select dayofyear ('1970-2-2 '); returns the day of the year.
Similar function: month () day () hour () week ()......
+ Date_add (date, interval 2 days) in a few days );
-Date_sub (date, interval 2 days) in a few days );
Time format:
Date_format (date, format)
Select DATE_FORMAT ('2017-10-04 22:23:00 ',' % W % M % Y ');
Other functions: TIME_TO_SEC () SEC_TO_TIME ()...

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.