Date/time calculations can be perform either on database side or in your server side language (PHP, ASP, C#) or even in Javascript. We’ll try to cover all those scenarios.
When copying and pasting code from this article to your application replace table name (mytable) and field names (posted, dob).
MySQL
Calculating age
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) from mytable AS age
30 days before date
select posted, DATE_SUB(posted, INTERVAL 30 DAY) from mytable
Date plus 30 days
select posted, DATE_ADD(posted, INTERVAL 30 DAY) from mytable
Difference between two dates in days
select posted, DATEDIFF(now(), posted) from mytableContinue Reading "Date/time handling in web applications"