php-mysqlHow to compare datetime in MySQL and PHP?
MySQL and PHP both have built-in functions to compare datetime values.
In MySQL, the DATE_SUB() function can be used to compare two datetime values. It returns the difference between two datetime values in seconds.
SELECT DATE_SUB(datetime1, datetime2)
Output example
-7200
In PHP, the strtotime() function can be used to compare two datetime values. It returns the difference between two datetime values in seconds.
$difference = strtotime($datetime1) - strtotime($datetime2);
Output example
-7200
Code explanation
DATE_SUB(): MySQL function to compare two datetime values and return the difference in secondsstrtotime(): PHP function to compare two datetime values and return the difference in seconds
Helpful links
More of Php Mysql
- How to list tables in PHP MySQL?
- How to get the version of MySQL using PHP?
- How to use MySQL transactions in PHP?
- How to join tables with PHP and MySQL?
- How to check the result of an insert in PHP and MySQL?
- How to output XML from MySQL using PHP?
- How to use a variable in a MySQL query using PHP?
- How to fetch data from MySQL in PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
See more codes...