phpmysql date operation notes

  • 2020-05-12 02:23:07
  • OfStack

When it comes to time comparison queries, int is significantly more efficient. Auspicious culture / / www. ofstack. com article / 29767. htm
But when we're working on a project or looking at the data directly in the database, it's obvious that this int1 is a big deal, like we want to
To view the registration time of a user:
select reg_time from t_xx_users where user_id=1;
At this time, the return is an int value, and the specific time cannot be intuitively seen. Therefore, the conversion of datetime and int is involved.
date and time of php are also involved in the corresponding transformation. This paper briefly summarizes 1:
(1)php
int value:
time(): is the number of seconds returned from Unix epoch (00:00:00 GMT, 1 January 1970) to the current time.
We want to obtain the number of seconds from January 1, 1970 to February 10, 2012 by strtotime() : strtotime('2012-2-10');
date value:
string date ( string format [, int timestamp] )
For example, directly date() returns the current implementation time, of course we can specify its format: 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 (12313223) '+ 1 day'); !!!!! 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 '); Return to day 1 of the week
select dayofmonth (' 2012-2-2 '); Return to the first day of January
select dayofyear (' 2012-2-2 '); Return to the first day of the year
Similar functions: month() day() hour() week()...
+ a few days date_add(date,interval 2 days);
- several 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()...

Related articles: