View Category

Parse a date and time from a string

Given the string "2008-05-06 13:29", parse it as a date representing 6th March, 2008 1:29:00pm in the local time zone.
php
echo date("jS F, Y g:i:sa", strtotime("2008-05-06 13:29")); // small version

Display information about a date

Display the day of month, day of year, month name and day name of the day 8 days from now.
php
$eightdays = strtotime("+8 days");

$dayofmonth = date("d", $eightdays); // 3
$dayofyear = date("z", $eightdays); // 183
$monthname = date("F", $eightdays); // July
$dayname = date("l", $eightdays); // Saturday
$eightdays = time() + 1 * 60 * 60 * 24 * 8;

$dayofmonth = date("d", $eightdays); // 3
$dayofyear = date("z", $eightdays); // 183
$monthname = date("F", $eightdays); // July
$dayname = date("l", $eightdays); // Saturday

Display a date in different locales

Display a language/locale friendly version of New Year's Day for 2009 for several languages/locales. E.g. for languages English, French, German, Italian, Dutch the output might be something like:

Thursday, January 1, 2009
jeudi 1 janvier 2009
giovedì 1 gennaio 2009
Donnerstag, 1. Januar 2009
donderdag 1 januari 2009

(Indicate in comments where possible if any language specific or operating system configuration needs to be in place.)
php
/* Be aware of that you need to have the locales installed */
setlocale(LC_TIME, "en_US");
echo strftime("%A, %B %e, %Y%n"); // %n = \n in strftime()
setlocale(LC_TIME, "fr_FR"); // French
echo strftime("%A, %B %e, %Y%n");
setlocale(LC_TIME, "de_DE"); // German
echo strftime("%A, %B %e, %Y%n");
setlocale(LC_TIME, "it_IT"); // Italian
echo strftime("%A, %B %e, %Y%n");
setlocale(LC_TIME, "nl_NL", "nld_nld"); // Dutch; Unix and Windows
echo strftime("%A, %B %e, %Y%n");
$locales = array("en_US", "fr_FR", "de_DE", "nl_NL", "it_IT");
foreach ($locales as $locale) {
setlocale(LC_TIME, $locale);
echo strftime("%A, %B %d %Y%n");
}

Display the current date and time

Create a Date object representing the current date and time. Print it out.
If you can also do this without creating a Date object you can show that too.
php
echo date('r') . "\n";
$d = new DateTime();
echo $d->format('r') . "\n";