Like most programming languages, PHP comes with a fairlyfull-featured API for date and time value manipulation. You've probablyused it in your applications, but never bothered to look too closely atit. Well, here's your chance to rectify that mistake - this articledelves into the date/time API in depth, uncovering some hidden nuggetsand demonstrating how it can be used to simplify date and timeprocessing in your PHP scripts.
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"));
?>