Home  >  Article  >  Backend Development  >  Detailed explanation of date comparison method in PHP and MYSQL

Detailed explanation of date comparison method in PHP and MYSQL

怪我咯
怪我咯Original
2017-07-11 14:34:132651browse

MYSQLWith dateFunctionnow(), this value returns not timestamp, but something like 2010-10- 1 12:12:12 is similar. PHP has a date function date(). date("Y-m-d", time()) returns the current time. If time() is simply used, the current time is returned. Time, for example, numbers such as 17

MYSQL date comparison

TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30;

Our date_col must be in date and time format, not a timestamp,

mysql> SELECT FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00'

day(FROM_UNIXTIME(1277709862)) This returns the number of days that have passed since a certain timestamp

Get the day: day(FROM_UNIXTIME(add_time))
Get the month: month(FROM_UNIXTIME(add_time))
Get the year: year(FROM_UNIXTIME(add_time))
Get the timestamp in sql text Then format it into day, month, and year: day(now()), month(now()), year(now())
where add_time is the field name, and the storage format is linuxtime Poke.

mysql> set @a = unix_timestamp();
Query OK, 0 rows affected (0.00 sec)
mysql> select @a;
+------ ------+
| @a |
+------------+
| 1154942084 |
+--------- ---+
1 row in set (0.00 sec)
mysql> select from_unixtime(@a);
+------------------- --+
| from_unixtime(@a) |
+---------------------+
| 2006-08-07 17: 14:44 |
+---------------------+
1 row in set (0.00 sec)

The above is the detailed content of Detailed explanation of date comparison method in PHP and MYSQL. For more information, please follow other related articles on the PHP Chinese website!

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