MySQL
  Home arrow MySQL arrow Page 5 - Practical Date/Time examples with PHP and ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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

Practical Date/Time examples with PHP and MySQL
By: Mauricio Cuenca
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 31
    2003-01-12

    Table of Contents:
  • Practical Date/Time examples with PHP and MySQL
  • The UNIX timestamp
  • How old are you? (exactly)
  • Dates with MySQL
  • Date Addition and Subtraction
  • Conclusion

  • 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


    Practical Date/Time examples with PHP and MySQL - Date Addition and Subtraction


    (Page 5 of 6 )

    Suppose that you have a members only area, each user has a password but you want the user to change his or her password every two weeks.

    The first thing you have to do is add a new column to your users table; this column will store the timestamp of the exact date when the user changed his or her password.

    Remember, again, that the timestamp is an unsigned integer, so keep this in mind when creating the table.

    The code for this approach should look like this:

    <?php

    $ndbconn = mysql_connect("localhost", "user", "password");
    mysql_select_db("mysql");

    $n_start_date = 1062521254; // Timestamp for 2003-09-02 11:47:34 taken from your DB

    // The query adds fourteen days to the given date
    $squery = "SELECT DATE_ADD(FROM_UNIXTIME($n_start_date), INTERVAL 14 DAY)";
    $nresult = mysql_query($squery);
    $s_new_date = mysql_result($nresult, 0, 0);

    // Free the result
    mysql_free_result($nresult);

    // Disconnected
    mysql_close($ndbconn);
    echo "Your password will expire on $s_new_date";

    ?>


    The main function here is DATE_ADD(), this MySQL function perfectly adds any number of seconds, minutes, hours, days or years to a specific date.

    In this example we used it to calculate the exact time fourteen days after the selected date. You can use this function with no risk, it even takes care of leap years.
    For example:

    mysql> SELECT DATE_ADD('2004-02-28 23:50:00', INTERVAL 10 MINUTE); #2004 is a leap year
    +-----------------------------------------------------+
    | DATE_ADD('2004-02-28 23:50:00', INTERVAL 10 MINUTE) |
    +-----------------------------------------------------+
    | 2004-02-29 00:00:00                                 |
    +-----------------------------------------------------+


    Subtraction is done with the DATE_SUB() function in the same way:

    mysql> SELECT DATE_SUB('2004-03-01', INTERVAL 1 DAY);
    +----------------------------------------+
    | DATE_SUB('2004-03-01', INTERVAL 1 DAY) |
    +----------------------------------------+
    | 2004-02-29                             |
    +----------------------------------------+


    And don’t worry about December 31st and January 1st transition:

    mysql> SELECT DATE_SUB('2004-01-01', INTERVAL 1 DAY);
    +----------------------------------------+
    | DATE_SUB('2004-01-01', INTERVAL 1 DAY) |
    +----------------------------------------+
    | 2003-12-31                             |
    +----------------------------------------+

    More MySQL Articles
    More By Mauricio Cuenca


       · Some good ideas for age calculation with birthdays before 1970-1-1?A big part of...
     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





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