Php&mysql Date Operation _php Tips

Source: Internet
Author: User
In time comparison query, the efficiency of the int is significantly higher. Xiang Wen See http://www.jb51.net/article/29767.htm
But when you do the project or directly in the database to view the data, it is obvious that the int is a big thing, such as we want to
To view the registration time for a user:
Select Reg_time from T_xx_users where user_id=1;
This time the return is an int value, not intuitive to see the specific times, so it involves the conversion of datetime and int problem,
and the date and time of PHP are also involved in the corresponding transformation. This article briefly summarizes:
(1) PHP
int Value:
Time (): is the number of seconds to return from the Unix era (GMT January 1, 1970 00:00:00) to the current period.
We want to obtain the number of seconds from January 1, 1970 to 2012-2-10 can be achieved by strtotime (): namely: Strtotime (' 2012-2-10 ');
Date Value:
String date (string format [, int timestamp])
For example: direct date () returns the implementation of the current time, of course we can specify his format: for example date (' y-m-d ', strtotime (' 2012-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));!! 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 (' 2012-2-2 '); Returns the first day of one weeks
Select DayOfMonth (' 2012-2-2 '); return to day of January
Select DayOfYear (' 2012-2-2 '); Returns the day of the year
Similar functions: Month () Day () hour () Week () ...
+ Days Date_add (Date,interval 2);
-Days Date_sub (Date,interval 2);
Time Format:
Date_format (Date,format)
Select Date_format (' 1997-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.