PHP
  Home arrow PHP arrow Page 2 - PHP Date + Time Primer
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  
Moblin 
JMSL Numerical Library 
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? 
PHP

PHP Date + Time Primer
By: Ryan Schwiebert
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2002-06-19

    Table of Contents:
  • PHP Date + Time Primer
  • Retrieving the server's timestamp
  • 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


    PHP Date + Time Primer - Retrieving the server's timestamp


    (Page 2 of 3 )

    No matter which operating system you are running your web server and PHP on, the procedure for gathering the current date and time is the same. The first problem is that the returned value is fairly cryptic.

    For example, the following PHP code:

    <? echo time(); ?>

    Will quickly translate into:

    1024334785

    Though this may not be very user friendly, it is an accurate measure of the date and time. This numeric date and time format originates from the fact that all Unix servers tell time by counting the number of seconds that have passed since 12:00 a.m. on January 1, 1970. All we need to do is learn how to display the above information in a way that makes sense to our site visitors.

    Displaying the date in a user friendly format
    Using PHP to convert the Unix timestamp into something more understandable is just a matter of employing the proper function. This tutorial will cover the use of a variety of basic functions available for this task.

    The most commonly used function is date(). As you might expect, this function is fairly customizable. Customization is achieved through PHP's recognition of a variety of characters submitted in the format string. Below are just a few common characters, for a complete list of them, visit the PHP documentation website.
    • D: day of the week
    • F: the month
    • h: hour, 12-hour format
    • H: hour, 24-hour format
    • i: minutes
    • l: day of the week
    • Y: year
    If we combine some of this knowledge into PHP code, the following:

    <?
    echo date("l F d Y h:i" ,time());
    ?>


    should give a result similar to:

    Monday June 17 2002 02:32

    Obviously this makes considerably more sense than the previous Unix timestamp. The result could be further improved through the addition of punctuation. When additional text is introduced into the format string it is important to remember to use a backslash to comment it out. Otherwise, your text will be recognized as part of the special character string.

    Perhaps a more elegant method of accomplishing tasks like this is to use the strftime() function instead. This function works exactly the same as the date() function except that a percent sign (%) is placed before each of the special characters in the format string. There are a few differences between the formatting characters so be sure to check the PHP documentation.

    The following characters are different from the previous date() examples:
    • %A: The day of the week
    • %B: The month in textual form
    Consider the following scenario. You are interested in including the date and/or time in a regular phrase to make the output more appealing to the site visitors.

    To do this, you could use the following code:

    <?
    echo strftime("Today is %A, %B %d, %Y." ,time());
    ?>


    The result is:

    Today is Monday, June 17, 2002.

    As you can see, in this instance, the strftime() function is slightly less cumbersome than the date() function.

    Outputting the date from an array
    Another method available for displaying the date and time is the getdate() function. This function takes the timestamp argument and creates an array of the date and time elements. Below are the array arguments that the getdate() function will accept.
    • "seconds": seconds
    • "minutes": minutes
    • "hours": hours
    • "mday": day of the month
    • "wday": numeric day of the week
    • "mon": month in a numeric format
    • "year": year
    • "yday": day of the year
    • "weekday": day of the week
    • "month": month in a textual format
    Using these array arguments, it is fairly simple to output the desired date and time elements. For example, this rather clunky PHP Code:

    <?
    $date_time = getdate (time());
    echo $date_time [ "month" ], " ", $date_time [ "mday" ], ", ", $date_time [ "year" ];
    ?>


    Will neatly display as:

    June 17, 2002

    The above example is admittedly not the best application for this function. Using the getdate() function becomes especially useful if you wish to display different aspects of the current date and time on completely different portions of a web page. Perhaps the following example will demonstrate this function's use more effectively.

    <html>
    <head>
    <title>Sample Getdate() function</title>
    </head>
    <body>
    <? $date_time = getdate (time()); ?>
    Hello! Welcome to our humble website. It is now <? echo $date_time [ "month" ] ?> of <? echo $date_time [ "year" ] ?>. It's hard to believe so much of this year has passed us by. It seems like only yesterday that
    </body>
    </html>


    The page would display an output similar to the following:

    Hello! Welcome to our humble website. It is now June of 2002. It's hard to believe so much of this year has passed us by. It seems like only yesterday that...

    More PHP Articles
    More By Ryan Schwiebert


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






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