info about link
: Javascript : PHP : MySQL Index :
date add string
: Source : : Explanation : : Example : : Todo : : Feedback :

Examples of the datetime_add_str / date_add_str / datetime_sub_str / date_sub_str functions

Example of MySQL's native functions

SELECT DATE_ADD(NOW(), INTERVAL 14 DAY);
SELECT DATE_ADD(NOW(), INTERVAL 3 HOUR);
SELECT DATE_ADD(NOW(), INTERVAL 2 YEAR);
SELECT DATE_ADD(NOW(), INTERVAL 1 MONTH);
SELECT DATE_SUB(NOW(), INTERVAL 2 WEEK);
SELECT DATE_SUB(NOW(), INTERVAL 3 MINUTE);
SELECT DATE_SUB(NOW(), INTERVAL 2 DAY);
SELECT DATE_SUB(NOW(), INTERVAL 1 YEAR);

Same example, using strings and the custom functions

SELECT DATETIME_ADD_STR(NOW(), 14, 'DAY');
SELECT DATETIME_ADD_STR(NOW(), 3, 'hour');
SELECT DATE_ADD_STR(NOW(), 2, 'Year');
SELECT DATE_ADD_STR(NOW(), 1, 'month');
SELECT DATETIME_SUB_STR(NOW(), 2, 'WEEK');
SELECT DATETIME_SUB_STR(NOW(), 3, 'minute');
SELECT DATE_SUB_STR(NOW(), 2, 'Day');
SELECT DATE_SUB_STR(NOW(), 1, 'year');
The output of this will be:
2012-06-04 01:47:08
2012-05-21 04:47:08
2014-05-21
2012-06-21
2012-05-07 01:47:08
2012-05-21 01:44:08
2012-05-19
2011-05-21
NOTE:
  • The above output is based on the current date of this server: 2012-05-21 01:47:08
  • The native function takes 2 arguments, this custom function takes 3 arguments (the quantity and unit-type are separate arguments); the INTERVAL keyword is not necessary.
  • The unit-types are case-insensitive.
=[Disclaimer]=     © 2005-2012 Excudo.net