Home  >  Article  >  Backend Development  >  php&mysql Date Operation Notes_PHP Tutorial

php&mysql Date Operation Notes_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:28749browse

In time comparison queries, int is significantly more efficient. See the auspicious article at http://www.jb51.net/article/29767.htm
But when working on a project or directly viewing the data in the database, it is obvious that this int is big at first sight. For example, we want to
To check the registration time of a user:
select reg_time from t_xx_users where user_id=1;
The return value at this time is an int value, and the specific time cannot be seen intuitively, so this time involves the conversion of datetime and int. Question,
The date and time of PHP also need to be converted accordingly. A brief summary of this article:
(1)php
int value:
time(): returns the time since the Unix epoch (00:00 GMT on January 1, 1970 :00) to the current time in seconds.
We want to get the number of seconds from January 1, 1970 to 2012-2-10, which can be achieved through strtotime(): that is: strtotime('2012-2-10');
date Value:
string date ( string format [, int timestamp] )
For example: directly date() returns the current time, of course we can specify its 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 for details [, 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 day of the week
select dayofmonth('2012-2-2'); returns The day of the month
select dayofyear('2012-2-2'); Returns the day of the year
Similar functions: month() day() hour() week().. ....
+date_add(date,interval 2 days);
-date_sub(date,interval 2 days);
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()...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325101.htmlTechArticleIn time comparison query, int is significantly more efficient. The auspicious article can be found at http://www.jb51.net/article/29767.htm But when working on a project or directly viewing data in the database, it is clear...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn