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 use a MySQL union in PHP?
- How to get the first row of a result in MySQL using PHP?
- How to convert a MySQL timestamp to a datetime in PHP?
- How to set a timeout for MySQL query in PHP?
- How to export data from MySQL to Excel using PHP?
- How to use MySQL transactions in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to keep a connection open in PHP and MySQL?
- How to order by a column in MySQL using PHP?
- How to use multiple prepared statements in PHP and MySQL?
See more codes...