Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Date/Time Processing With PHP
Simplify date and time processing in your PHP scripts

| When Looks Do Matter |

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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
It is now 12:20 20 Feb 2002
').'
'

The 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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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,


<?
// example: returns "12:28 pm 20 Feb 2002"
echo date("h:i a d M Y", mktime());

// example: returns "12:28 20 February 2002"
echo date("H:i d F Y", mktime());

// example: 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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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"));
?>



How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Creating Web Calendars With The PEAR Calendar Class
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback