PHP
  Home arrow PHP arrow Page 5 - Date/Time Processing with PHP
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? 
PHP

Date/Time Processing with PHP
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2002-03-19

    Table of Contents:
  • Date/Time Processing with PHP
  • Getting A Date
  • A Stamp In Time...
  • Race Against Time
  • When Looks Do Matter
  • Checking Up
  • Turning The Tables

  • 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/Time Processing with PHP - When Looks Do Matter


    (Page 5 of 7 )

    Once you've got yourself a timestamp, you can use the date() function to format it and make it look pretty. This date() function is easily one of the most useful functions in this collection - it allows you to massage that long, ugly timestamp into something that's a pleasure to read.

    Here's an example:
    <?
    // format timestamp with date()
    echo "It is now " . date("h:i d M Y", mktime());
    ?>
    The output of this would be: It is now 12:20 20 Feb 2002The date() function accepts two arguments: a format string and a timestamp. This format string is a sequence of characters, each of which has a special meaning. Here's a quick list:
    CHARACTER            WHAT IT MEANS
    ---------------------------------------------------------
    d   day of the month (numeric) 
    D   day of the week (string)
    F   month (string)
    m   month (numeric)
    Y   year
    h   hour (in 12-hour format)
    H   hour (in 24-hour format)
    a   AM or PM
    i   minute
    s   second
    This is just a brief list, take a look at the PHP manual for a complete list.

    Using these special characters, it's possible to format a timestamp to display just the information you want. For example,
    <?
    // returns "12:28 pm 20 Feb 2002"
    echo date("h:i a d M Y", mktime());
    // returns "12:28 20 February 2002"
    echo date("H:i d F Y", mktime());
    // returns "02-20-2002"
    echo date("m-d-Y", mktime());
    ?>
    You can use date() in combination with mktime() to generate human-readable strings for any arbitrary date value. A common example of this involves using date() on MySQL DATETIME values, which are typically in the format
    yyyy-mm-yy hh:mm:ss 
    Here's an example of how a MySQL value could be converted into a human-readable date value:
    <?
    // format MySQL DATETIME value into a more readable string function
    formatDate($val) {
    $arr = explode("-", $val);
    return date("d M Y", mktime(0,0,0, $arr[1], $arr[2], $arr[0]));
    }
    // assume I got the value "2001-02-01 17:29:25" from a query
    // this is too much information
    // use formatDate() to clean it up
    echo formatDate("2001-02-01 17:29:25")
    // the output would be "01 Feb 2001"
    ?>
    In the event that you want to add your own printable characters to the string, you might need to use single quotes around the format string and escape them with a backslash so that PHP does not get confused. Consider the following example, which illustrates the difference:
    <?
    // would generate "028 2345 2o3 12:23 20 Feb 2002"
    echo date("It is now h:i d M Y", mktime());
    // would generate "It is now 12:23 20 Feb 2002"
    echo date('\I\t\ \i\s \n\o\w\ h:i d M Y', mktime());
    ?>
    You might also like to take a look at the fairly cool strtotime() function, which can be used to convert any natural-language date or time statement into a UNIX timestamp. Here are a few examples:
    <?
    // returns "20 Feb 2002"
    echo date("d M Y", strtotime("now"));
    // returns "21 Feb 2002"
    echo date("d M Y", strtotime("tomorrow"));
    // returns "27 Feb 2002"
    echo date("d M Y", strtotime("next week"));
    // returns "05:00 14 Feb 2002"
    echo date("h:i d M Y", strtotime("5 pm 6 days ago"));
    ?>

    More PHP Articles
    More By The Disenchanted Developer, (c) Melonfire


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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