Home arrow PHP arrow Page 5 - Date/Time Processing with PHP

When Looks Do Matter - PHP

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.

  1. Date/Time Processing with PHP
  2. Getting A Date
  3. A Stamp In Time...
  4. Race Against Time
  5. When Looks Do Matter
  6. Checking Up
  7. Turning The Tables
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 14
March 19, 2002

print this article


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: