Useful PHP Functions Part 2

Last month I highlighted a few PHP functions that I use regularly. Because the language is so large I have decided to expand upon my previous post. Specifically I would like to cover some useful date based functions.

Date($format,$optionalTimestamp)

If you’re ever asked to program anything to do with date and time this is the function you are looking for. Simply put it will return a date. The first parameter allows you to set a format you wish the value to be returned in. The second parameter allows you to pass a time stamp. This can be used to change the format of a date variable you may already have.

// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01

strtotime($time)

The above function takes a string containing a date in any format and converts it to a Unix Timestamp. The first parameter contains the date time value you wish to convert. This function even allows you to pass phrases such as “Last Monday” or “Now” as the $time variable.

echo strtotime("now"), "n";
echo strtotime("10 September 2000"), "n";

getDate($timestamp)

getDate returns an array containing date information. It allows you to pass a time stamp.

$today = getdate();
print_r($today);