MySQL
  Home arrow MySQL arrow Page 3 - 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 
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 / 32
    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 - How old are you? (exactly)


    (Page 3 of 6 )

    Using the date_diff() function that we’ve just created it's very easy to calculate your exact age by how many years, days, hours, minutes and seconds since you were born:

    <?php

    // Calculating my exact age
    $str_birthday = "1978-04-26"; // My Birth Day
    $str_today = date("Y-m-d H:i:s"); // The exact time
    date_diff($str_birthday, $str_today);

    ?>


    As you see I’m using the same function but changing the parameters a little. First $str_birthday is a fixed string, in this example my birthday.

    You can, for example, ask the user through a form and then call the function like this:

    <?php date_diff($_POST[‘birthday’], date("Y-m-d H:i:s")); ?>

    The trick here is the date() function. With its given parameters prints the exact date of today in the format YYYY-MM-DD hh:mm:ss.

    The code returns:
    9024 days, 17 hours, 29 minutes, 57 seconds

    How many days until…?

    Again, using the same function that we created at the beginning and changing the parameters appropriately we can calculate how many days until a given date. There is a problem though; the UNIX timestamp only works for dates between January 1st, 1970 and December 31st, 2037. Dates outside that range will give an unpredictable result.

    Now, hoping that we'll come up with a solution for this in the next thirty years let's do the math:

    <?php

    $str_future_date = "2028-04-26"; // I'll be fifty this day!
    date_diff(date("Y-m-d H:i:s"), $str_future_date);

    ?>


    At this point you should be familiar with the function, just tweaking the parametersa little. In this case the first parameter is the exact date of today and the second is the future date that we want to know how many days until.

    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 ...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek