MySQL TIMESTAMPDIFF

2008-10-07

Timestampdiff is a very useful function in MySQL. It counts days, months, years or what you want between two dates. Of course it takes into account things like 29th February etc. The usage is simple:

mysql> SELECT TIMESTAMPDIFF(DAY, '2008-02-28', '2008-03-01');
//prints: 2


You can for example count the number of days you are on the world.

mysql> SELECT TIMESTAMPDIFF(DAY, '1991-01-01', DATE_FORMAT(NOW(),"%Y-%m-%d"));
//prints: 6489


Or sorry, you actually wanted to have the time in seconds.

mysql> SELECT TIMESTAMPDIFF(SECOND, '1991-01-01 15:00:00', DATE_FORMAT(NOW(),"%Y-%m-%d %H:%I:%S"));
//prints the time since 1991-01-01 15:00:00 until now

0 komentářů:

About This Blog

Lorem Ipsum

  © Free Blogger Templates Nightingale by Ourblogtemplates.com 2008

Back to TOP