MySQL
  Home arrow MySQL arrow Page 5 - Date Arithmetic With MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 35
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Artificial Intelligence
    ( Page 5 of 8 )

    It should be noted that MySQL includes intelligence to automatically format the result of the calculation as either a date-only or date-and-time value. For example, if your arguments to DATE_ADD() or DATE_SUB() contains only year, month and day components, MySQL will output a date-only value as result.


    mysql> SELECT DATE_SUB(20041130, INTERVAL 3 MONTH);
    +--------------------------------------+
    | DATE_SUB(20041130, INTERVAL 3 MONTH) |
    +--------------------------------------+
    | 2004-08-30 |
    +--------------------------------------+
    1 row in set (0.00 sec)

    However, if your arguments include a time component - hours, minutes or seconds - then MySQL produces a result containing both date and time components.


    mysql> SELECT DATE_ADD('2010-02-14', INTERVAL "1 1" DAY_HOUR);
    +-------------------------------------------------+
    | DATE_ADD('2010-02-14', INTERVAL "1 1" DAY_HOUR) |
    +-------------------------------------------------+
    | 2010-02-15 01:00:00 |
    +-------------------------------------------------+
    1 row in set (0.01 sec)

    If you use illegal date or time values, MySQL will still attempt to return a valid result by performing adjustments on the various values.


    mysql> SELECT DATE_ADD('2010-02-30', INTERVAL 1 DAY);
    +----------------------------------------+
    | DATE_ADD('2010-02-30', INTERVAL 1 DAY) |
    +----------------------------------------+
    | 2010-03-03 |
    +----------------------------------------+
    1 row in set (0.00 sec)

    MySQL also provides an alternative syntax to DATE_ADD() and DATE_SUB(), which is sometimes more readable - this involves using + and - signs to indicate the type of calculation to be performed. Consider the
    following examples, which illustrate:


    mysql> SELECT 20000615 + INTERVAL 5 DAY;
    +---------------------------+
    | 20000615 + INTERVAL 5 DAY |
    +---------------------------+
    | 2000-06-20 |
    +---------------------------+
    1 row in set (0.00 sec)

    mysql> SELECT 20000615000000 - INTERVAL 10 SECOND ;
    +-------------------------------------+
    | 20000615000000 - INTERVAL 10 SECOND |
    +-------------------------------------+
    | 2000-06-14 23:59:50 |
    +-------------------------------------+
    1 row in set (0.00 sec)

    mysql> SELECT '2000-06-15 13:00' - INTERVAL "2-1" YEAR_MONTH;
    +------------------------------------------------+
    | '2000-06-15 13:00' - INTERVAL "2-1" YEAR_MONTH |
    +------------------------------------------------+
    | 1998-05-15 13:00:00 |
    +------------------------------------------------+
    1 row in set (0.00 sec)



     
     
    >>> More MySQL Articles          >>> More By icarus, (c) Melonfire
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT