MySQL
  Home arrow MySQL arrow Page 4 - Date Arithmetic With MySQL
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Date Arithmetic With MySQL
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 33
    2003-07-03

    Table of Contents:
  • Date Arithmetic With MySQL
  • When Two And Two Don't Make Four
  • Counting Down
  • The Number Game
  • Artificial Intelligence
  • A Short Interval
  • Lather, Rinse, Repeat
  • Code Poet

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Date Arithmetic With MySQL - The Number Game


    (Page 4 of 8 )

    In addition to futzing with days and dates, MySQL also provides you with the ability to perform date arithmetic on specific date and time values, with its DATE_ADD() and DATE_SUB() functions. The syntax of these functions is somewhat more complex than the ones you've seen thus far - here's what it looks like:


    DATE_ADD(startDate, INTERVAL period periodType)

    DATE_SUB(startDate, INTERVAL period periodType)

    In order to better understand this, consider the following example, which adds 1 year to the specified start date and returns the new value:


    mysql> SELECT DATE_ADD('2003-04-15', INTERVAL 1 YEAR);
    +-----------------------------------------+
    | DATE_ADD('2003-04-15', INTERVAL 1 YEAR) |
    +-----------------------------------------+
    | 2004-04-15 |
    +-----------------------------------------+
    1 row in set (0.18 sec)

    Here's another one, this one adding 3 hours and 45 minutes to the starting timestamp,


    mysql> SELECT DATE_ADD('2003-04-15 02:02', INTERVAL "03:45"
    mysql> HOUR_MINUTE);
    +------------------------------------------------------------+
    | DATE_ADD('2003-04-15 02:02', INTERVAL "03:45" HOUR_MINUTE) |
    +------------------------------------------------------------+
    | 2003-04-15 05:47:00 |
    +------------------------------------------------------------+
    1 row in set (0.01 sec)

    and yet another, this one adding 7 days, 1 hour, 55 minutes and 10 seconds to December 25 2000:


    mysql> SELECT DATE_ADD('2000-12-25 12:00:00', INTERVAL "7 01:55:10"
    DAY_SECOND);
    +-------------------------------------------------------------------+
    | DATE_ADD('2000-12-25 12:00:00', INTERVAL "7 01:55:10" DAY_SECOND) |
    +-------------------------------------------------------------------+
    | 2001-01-01 13:55:10 |
    +-------------------------------------------------------------------+
    1 row in set (0.01 sec)

    In order to better understand this, it's necessary to delve a little deeper into the syntax of the DATE_ADD() and DATE_SUB() functions:

    1. The first parameter, startDate, is a date or time value in either string ("YYYY-MM-DD HH:MM:SS") or number (YYYYMMDDHHMMSS) format. This parameter specifies the date to use as a base for the calculation.

    2. The second parameter consists of three separate components, which together specify the interval which is to be added to (or subtracted from) the first parameter. The components consist of the keyword INTERVAL, followed by the interval period and a keyword providing information on the interval calculation to be performed.

    The relationship between the formatting of the interval period and the keyword following it is fixed, and illustrated in the following table:


    period (formatted as) periodType
    ---------------------------------------------
    SECONDS SECOND
    MINUTES MINUTE
    HOURS HOUR
    DAYS DAY
    MONTHS MONTH
    YEARS YEAR
    "MINUTES:SECONDS" MINUTE_SECOND
    "HOURS:MINUTES" HOUR_MINUTE
    "DAYS HOURS" DAY_HOUR
    "YEARS-MONTHS" YEAR_MONTH
    "HOURS:MINUTES:SECONDS" HOUR_SECOND
    "DAYS HOURS:MINUTES" DAY_MINUTE
    "DAYS HOURS:MINUTES:SECONDS" DAY_SECOND

    With this in mind, the examples above should become much clearer. If, for example, you wanted to perform a calculation involving interval units of 1 year, you could format your arguments using the YEAR type, whereas if you wanted to perform date arithmetic with hours and minutes, you could format your arguments using the HOUR_MINUTE or HOUR_SECOND types.

    Let's look at a few more examples of this in action.


    mysql> SELECT DATE_ADD(20041130, INTERVAL 1 MONTH);
    +--------------------------------------+
    | DATE_ADD(20041130, INTERVAL 1 MONTH) |
    +--------------------------------------+
    | 2004-12-30 |
    +--------------------------------------+
    1 row in set (0.88 sec)

    mysql> SELECT DATE_ADD(20041130, INTERVAL 31 DAY);
    +-------------------------------------+
    | DATE_ADD(20041130, INTERVAL 31 DAY) |
    +-------------------------------------+
    | 2004-12-31 |
    +-------------------------------------+
    1 row in set (0.02 sec)

    mysql> SELECT DATE_ADD('2003-09-17 03:30:00', INTERVAL "02:30"
    HOUR_MINUTE);
    +---------------------------------------------------------------+
    | DATE_ADD('2003-09-17 03:30:00', INTERVAL "02:30" HOUR_MINUTE) |
    +---------------------------------------------------------------+
    | 2003-09-17 06:00:00 |
    +---------------------------------------------------------------+
    1 row in set (0.01 sec)

    mysql> SELECT DATE_SUB('2003-01-01', INTERVAL 24 HOUR);
    +------------------------------------------+
    | DATE_SUB('2003-01-01', INTERVAL 24 HOUR) |
    +------------------------------------------+
    | 2002-12-31 00:00:00 |
    +------------------------------------------+
    1 row in set (0.00 sec)

    mysql> SELECT DATE_SUB('2003-09-17 12:00:00', INTERVAL "1 12" DAY_HOUR);

    +-----------------------------------------------------------+
    | DATE_SUB('2003-09-17 12:00:00', INTERVAL "1 12" DAY_HOUR) |
    +-----------------------------------------------------------+
    | 2003-09-16 00:00:00 |
    +-----------------------------------------------------------+
    1 row in set (0.00 sec)

    mysql> SELECT DATE_SUB('2003-01-01 18:45:10', INTERVAL "17:45:10"
    HOUR_SECOND);
    +------------------------------------------------------------------+
    | DATE_SUB('2003-01-01 18:45:10', INTERVAL "17:45:10" HOUR_SECOND) |
    +------------------------------------------------------------------+
    | 2003-01-01 01:00:00 |
    +------------------------------------------------------------------+
    1 row in set (0.00 sec)

    More MySQL Articles
    More By icarus, (c) Melonfire


     

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway