A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

MySQL: Get difference between 2 dates

Fairly simple and I’m sure this would come in handy in the future. This is one of those functions that I thought did not exist. :P I should RTFM more…

SELECT DATEDIFF('2009-03-31 23:59:59','2009-03-29');
/*would output: 2*/
 
/*Another usage would be something like: */
SELECT p.* FROM
		people p
	WHERE
		p.country LIKE 'Phil%'
                AND
                DATEDIFF(p.travel_date_to,p.travel_date_from)>=15
/*
In this scenario, the query would give me a list of people who 
are on vacation in the Philippines for more than 15 days. 
I used the travel_date_to and travel_date_from fields.
*/
 
/*
How many days old am I?
*/
SELECT DATEDIFF(curdate(), '1983-10-29' );
/* would output 9281 days :P */
This entry was posted in General and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>