Date and Time functions
Date/time
checkdate
Validate a gregorian date/time
Description
boolcheckdate
intmonth
intday
intyear
Returns &true; if the date given is valid; otherwise returns &false;.
Checks the validity of the date formed by the arguments. A date
is considered valid if:
year is between 1 and 32767 inclusive
month is between 1 and 12 inclusive
Day is within the allowed number of
days for the given month. Leap
years are taken into consideration.
See also mktime and strtotime.
date
Format a local time/date
Description
stringdate
stringformat
int
timestamp
Returns a string formatted according to the given format string using the
given integer timestamp or the current local time
if no timestamp is given.
The valid range of a timestamp is typically from Fri, 13 Dec
1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are
the dates that correspond to the minimum and maximum values for
a 32-bit signed integer.)
To generate a timestamp from a string representation of the date, you
may be able to use strtotime. Additionally, some
databases have functions to convert their date formats into timestamps
(such as MySQL's UNIX_TIMESTAMP function).
The following characters are recognized in the format string:
a - "am" or "pm"
A - "AM" or "PM"
B - Swatch Internet time
d - day of the month, 2 digits with leading zeros; i.e. "01"
to "31"
D - day of the week, textual, 3 letters; i.e. "Fri"
F - month, textual, long; i.e. "January"
g - hour, 12-hour format without leading zeros; i.e. "1" to
"12"
G - hour, 24-hour format without leading zeros; i.e. "0" to
"23"
h - hour, 12-hour format; i.e. "01" to "12"
H - hour, 24-hour format; i.e. "00" to "23"
i - minutes; i.e. "00" to "59"
I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
j - day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') - day of the week, textual, long;
i.e. "Friday"
L - boolean for whether it is a leap year; i.e. "0" or "1"
m - month; i.e. "01" to "12"
M - month, textual, 3 letters; i.e. "Jan"
n - month without leading zeros; i.e. "1" to "12"
O - Difference to Greenwich time in hours; i.e. "+0200"
r - RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200"
(added in PHP 4.0.4)
s - seconds; i.e. "00" to "59"
S - English ordinal suffix for the day of the month, 2 characters;
i.e. "th", "nd"
t - number of days in the given month; i.e. "28" to "31"
T - Timezone setting of this machine; i.e. "MDT"
U - seconds since the epoch
w - day of the week, numeric, i.e. "0" (Sunday) to "6"
(Saturday)
W - ISO-8601 week number of year, weeks starting on monday (added in PHP 4.1.0)
(Saturday)
Y - year, 4 digits; i.e. "1999"
y - year, 2 digits; i.e. "99"
z - day of the year; i.e. "0" to "365"
Z - timezone offset in seconds (i.e. "-43200" to "43200"). The
offset for timezones west of UTC is always negative, and for
those east of UTC is always positive.
Unrecognized characters in the format string will be printed
as-is. The "Z" format will always return "0" when using
gmdate.
date example
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.
Escaping characters in date
It is possible to use date and
mktime together to find dates in the future
or the past.
date and mktime
example
This can be more reliable than simply adding or subtracting the number
of seconds in a day or month to a timestamp because of daylight savings
time.
Some examples of date formatting. Note that
you should escape any other characters, as any which currently
have a special meaning will produce undesirable results, and
other characters may be assigned meaning in future PHP versions.
When escaping, be sure to use single quotes to prevent characters
like \n from becoming newlines.
date Formatting
To format dates in other languages, you should use the
setlocale and strftime
functions.
See also getlastmod, gmdate,
mktime, strftime
and time.
getdate
Get date/time information
Description
arraygetdate
inttimestamp
Returns an associative array containing the date information of
the timestamp, or the current local time if
no timestamp is given, as the following array
elements:
"seconds" - seconds
"minutes" - minutes
"hours" - hours
"mday" - day of the month
"wday" - day of the week, numeric : from 0 as Sunday up to 6 as Saturday
"mon" - month, numeric
"year" - year, numeric
"yday" - day of the year, numeric; i.e. "299"
"weekday" - day of the week, textual, full; i.e. "Friday"
"month" - month, textual, full; i.e. "January"
getdate example
gettimeofday
Get current time
Description
arraygettimeofday
This is an interface to gettimeofday(2). It returns an
associative array containing the data returned from the system
call.
"sec" - seconds
"usec" - microseconds
"minuteswest" - minutes west of Greenwich
"dsttime" - type of dst correction
gmdate
Format a GMT/CUT date/time
Description
stringgmdate
stringformat
inttimestamp
Identical to the date function except that
the time returned is Greenwich Mean Time (GMT). For example, when
run in Finland (GMT +0200), the first line below prints "Jan 01
1998 00:00:00", while the second prints "Dec 31 1997 22:00:00".
gmdate example
See also date, mktime,
gmmktime and strftime.
gmmktime
Get UNIX timestamp for a GMT date
Description
intgmmktime
inthour
intminute
intsecond
intmonth
intday
intyear
intis_dst
Identical to mktime except the passed
parameters represents a GMT date.
gmstrftime
Format a GMT/CUT time/date according to locale settings
Description
stringgmstrftime
stringformat
inttimestamp
Behaves the same as strftime except that the
time returned is Greenwich Mean Time (GMT). For example, when run
in Eastern Standard Time (GMT -0500), the first line below prints
"Dec 31 1998 20:00:00", while the second prints "Jan 01 1999
01:00:00".
gmstrftime example
See also strftime.
localtime
Get the local time
Description
arraylocaltime
int
timestamp
bool
is_associative
The localtime function returns an array
identical to that of the structure returned by the C function
call. The first argument to localtime is
the timestamp, if this is not given the current time is used.
The second argument to the localtime is the
is_associative, if this is set to 0 or not
supplied than the array is returned as a regular, numerically
indexed array. If the argument is set to 1 then
localtime is an associative array containing
all the different elements of the structure returned by the C
function call to localtime. The names of the different keys of
the associative array are as follows:
"tm_sec" - seconds
"tm_min" - minutes
"tm_hour" - hour
"tm_mday" - day of the month
"tm_mon" - month of the year, starting with 0 for January
"tm_year" - Years since 1900
"tm_wday" - Day of the week
"tm_yday" - Day of the year
"tm_isdst" - Is daylight savings time in effect
microtime
Return current UNIX timestamp with microseconds
Description
stringmicrotime
Returns the string "msec sec" where sec is the current time
measured in the number of seconds since the Unix Epoch (0:00:00
January 1, 1970 GMT), and msec is the microseconds part. This
function is only available on operating systems that support the
gettimeofday() system call.
Both portions of the string are returned in units of seconds.
microtime example
See also time.
mktime
Get UNIX timestamp for a date
Description
intmktime
inthour
intminute
intsecond
intmonth
intday
intyear
intis_dst
Warning: Note the strange order of
arguments, which differs from the order of arguments in a regular
UNIX mktime() call and which does not lend itself well to leaving
out parameters from right to left (see below). It is a common
error to mix these values up in a script.
Returns the Unix timestamp corresponding to the arguments
given. This timestamp is a long integer containing the number of
seconds between the Unix Epoch (January 1 1970) and the time
specified.
Arguments may be left out in order from right to left; any
arguments thus omitted will be set to the current value according
to the local date and time.
is_dst can be set to 1 if the time is
during daylight savings time, 0 if it is not, or -1 (the default)
if it is unknown whether the time is within daylight savings time
or not. If it's unknown, PHP tries to figure it out itself. This can
cause unexpected (but not incorrect) results.
is_dst was added in 3.0.10.
mktime is useful for doing date arithmetic
and validation, as it will automatically calculate the correct
value for out-of-range input. For example, each of the following
lines produces the string "Jan-01-1998".
mktime example
Year may be a two or four digit value,
with values between 0-69 mapping to 2000-2069 and 70-99 to
1970-1999 (on systems where time_t is a 32bit signed integer, as
most common today, the valid range for
year is somewhere between 1902 and 2037).
The last day of any given month can be expressed as the "0" day
of the next month, not the -1 day. Both of the following examples
will produce the string "The last day in Feb 2000 is: 29".
Last day of next month
Date with year, month and day equal to zero is considered illegal
(otherwise it what be regarded as 30.11.1999, which would be strange
behavior).
See also date and time.
strftime
Format a local time/date according to locale settings
Description
stringstrftime
stringformat
int
timestamp
Returns a string formatted according to the given format string
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 conversion specifiers are recognized in the format
string:
%a - abbreviated weekday name according to the current locale
%A - full weekday name according to the current locale
%b - abbreviated month name according to the current locale
%B - full month name according to the current locale
%c - preferred date and time representation for the current
locale
%C - century number (the year divided by 100 and truncated to
an integer, 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 corresponding to the ISO week number (see %V).
This has the same format and value as %Y, except that if the ISO week
number belongs to the previous or next year, that year is used
instead.
%h - same as %b
%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' according to the given time value, or
the corresponding strings for the current locale
%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
Sun Solaris seems to start with Sunday as 1
although ISO 9889:1999 (the current C standard) clearly
specifies that it should be Monday.
%U - week number of the current year as a decimal number,
starting with the first Sunday as the first day of the first
week
%V - The ISO 8601:1988 week number of the current year as a
decimal number, range 01 to 53, where week 1 is the first
week that has at least 4 days in the current year, and with
Monday as the first day of the week. (Use %G or %g for the year
component that corresponds to the week number for the specified
timestamp.)
%W - week number of the current year as a decimal number,
starting with the first Monday as the first day of the first
week
%w - day of the week as a decimal, Sunday being 0
%x - preferred date representation for the current locale
without the time
%X - preferred time representation for the current 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
Not all conversion specifiers may be supported by your C
library, in which case they will not be supported by PHP's
strftime. This means that %T and %D will
not work on Windows.
strftime example
This example works if you have the respective locales installed
in your system.
See also setlocale and
mktime and the
Open Group specification of
strftime.
time
Return current UNIX timestamp
Description
inttime
Returns the current time measured in the number of seconds since
the Unix Epoch (January 1 1970 00:00:00 GMT).
See also date.
strtotime
Parse about any English textual datetime description into a UNIX
timestamp
Description
intstrtotime
stringtime
intnow
The function expects to be given a string containing an English date
format and will try to parse that format into a UNIX timestamp relative
to the timestamp given in now, or the current time
if none is supplied. Upon failure, -1 is returned.
Because strtotime behaves according to GNU
date syntax, have a look at the GNU manual page titled
Date Input Formats.
Described there is valid syntax for the time
parameter.
strtotime examples
Checking for failure
The valid range of a timestamp is typically from Fri, 13 Dec
1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are
the dates that correspond to the minimum and maximum values for
a 32-bit signed integer.)