Contents / Previous / Next


Date and Time Functions

These functions allow you to get the date and time from the server where your PHP scripts are running. The first function we discuss is date(): string date ( string format [, int timestamp]) It returns a string formatted according to the given format using the given timestamp or the current local time if no timestamp is given.
Example: echo "Today is ".date("l")." the " .date("d")." of ".date("F")."<p>"; //Outputs: Today is Saturday the 24 of May echo date ("l dS of F Y h:i:s A")."<p>"; //Outputs: Saturday 24th of May 2003 11:44:19 AM You can format the date and time in many different ways (and more):
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
d Day of the month, 2 digits with leading zeros 01 to 31
D day as three letters Mon through Sun
F month  as January or March January through December
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
j Day of the month without leading zeros 1 to 31
l day of the week Sunday through Saturday
L Whether it's a leap year 1, 0 otherwise.
m Numeric month, 01 through 12
M month as letters Jan through Dec
n Numeric month, without leading zeros 1 through 12
r RFC 822 formatted date Expl: Thu, 21 Dec 2000 16:01:07 +0200
s Seconds, with leading zeros 00 through 59
S English ordinal suffix for the day of the month, 
  2 characters st, nd, rd or th. Works well with j
t Number of days in the given month 28 through 31
T Timezone setting of this machine Examples: EST, MDT ...
U Seconds since the Unix Epoch 
w Numeric day of the week 0 (Sunday) through 6 (Saturday)
W ISO-8601 week number of year
Y full numeric year, 4 digits Examples: 1999 or 2003
y A two year Examples: 99 or 03
z The day of the year 0 through 366
Z Timezone offset in seconds. 
You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash. If the character with a backslash is already a special sequence, you may need to also escape the backslash. Example: echo date("l \\t\h\e jS"); //Outputs: Saturday the 24th


Create a timestamp for a date: mktime

mktime() returns the Unix timestamp corresponding to the arguments given: int mktime ( [ int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst ]]]]]]]) Arguments may be left out in order from right to left.
Warning: The order of arguments, differs from a UNIX mktime() call.

mktime() is useful in combination with date() or strftime():

echo date ("M-d-Y", mktime (0,0,0,12,32,1997)); $lastday = mktime (0,0,0,3,0,2000); echo strftime ("<P>Last day in Feb 2000 is: %d<P>", $lastday); "0" stands for the last day of a month.


Alternative Functions: getdate

array getdate ( [int timestamp] ): Returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given, as the following associative array elements:
"seconds" seconds 0 to 59
"minutes" minutes 0 to 59
"hours"   hours 0 to 23
"mday"    the day of the month 1 to 31
"wday"    the day of the week 0 (for Sunday) 
          through 6 (for Saturday)
"mon"     a month 1 through 12
"year"    year, 4 digits Examples: 1999 or 2003
"yday"    day of the year 0 through 366
"weekday" day of the week Sunday through Saturday
"month"   month, January through December
Example: $today = getdate(); $month = $today['month']; $mday = $today['mday']; $year = $today['year']; echo "$month $mday, $year"; //Outputs: May 24, 2003


Other Languages: setlocale

To format dates in other languages, use the setlocale() and strftime() functions.

string setlocale ( mixed category, string locale [, string ...]):
Category is a named constant (or string) specifying the category of the functions affected by the locale setting:

LC_ALL      for all of the below
LC_TIME     for date and time formatting with strftime()
LC_COLLATE  for string comparison, see strcoll()
LC_CTYPE    for character classification and conversion
LC_MONETARY for localeconv()
LC_NUMERIC  for decimal separator
locale is the language string, for example "nl_NL" or "de_DE", if zero or "0", the current setting is returned. Setlocale returns the new current locale, or FALSE if the locale functionality is not implemented in the platform.

Example:

/* Set locale to Dutch */ setlocale (LC_ALL, 'nl_NL'); /* Output: vrijdag 22 december 1978 */ echo strftime ("%A %e %B %Y", mktime (0, 0, 0, 12, 22, 1978)); /* try different possible locale names for German */ $loc_de = setlocale (LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge'); echo "Preferred locale for German on this system is '$loc_de'"; If locale is followed by additional parameters then each is tried to be set as new locale until success.
This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.


string strftime ( string format [, int timestamp]):
Formats a local time/date according to locale settings using the given timestamp or the current local time if no timestamp is given.
Month and weekday names and other language dependent strings respect the current locale set with setlocale().
The following (and more ) conversion specifiers are recognized in the format string:

%a - abbreviated weekday name
%A - full weekday name
%b - abbreviated month name
%B - full month name
%c - preferred date and time representation for the current locale
%C - century number (range 00 to 99)
%d - day of the month as a decimal number (range 01 to 31)
%D - same as %m/%d/%y
%e - day of the month as a decimal number, 
     a single digit is preceded by a space (range ' 1' to '31')
%g - like %G, but without the century.
%G - The 4-digit year
%H - hour as a decimal number using a 24-hour clock (range 00 to 23)
%I - hour as a decimal number using a 12-hour clock (range 01 to 12)
%j - day of the year as a decimal number (range 001 to 366)
%m - month as a decimal number (range 01 to 12)
%M - minute as a decimal number
%n - newline character
%p - either `am' or `pm'
%r - time in a.m. and p.m. notation
%R - time in 24 hour notation
%S - second as a decimal number
%t - tab character
%T - current time, equal to %H:%M:%S
%u - weekday as a decimal number [1,7], with 1 representing Monday
%U, %V, %W  - different week numbers of the current year, 
%w - day of the week as a decimal, Sunday being 0
%x - preferred date representation for locale without the time
%X - preferred time representation for locale without the date
%y - year as a decimal number without a century (range 00 to 99)
%Y - year as a decimal number including the century
%Z - time zone or name or abbreviation
%% - a literal `%' character
Example: setlocale (LC_TIME, "fr_FR"); print (strftime ("%A, in French ")); setlocale (LC_TIME, "de_DE"); print (strftime ("%A, in German "));


UNIX timestamp

int time ( void): Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

string microtime ( void): Returns current UNIX timestamp with microseconds as the string "msec sec". Example:

<?php function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); echo "getmicrotime: ".(float)$usec . " __ ". (float)$sec."<BR>"; return ((float)$usec + (float)$sec); } $time_start = getmicrotime(); for ($i=0; $i < 1000; $i++){ //do nothing, 1000 times } $time_end = getmicrotime(); $time = $time_end - $time_start; echo "Did nothing in $time seconds"; ?>