Date/Time Functions
Date/Time Functions
checkdate
Name
checkdate — validate a date/time
Description
int checkdate(int month, int day, int year);
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 1900 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.
date
Name
date — format a local time/date
Description
string date(string format, 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.
The following characters are recognized in the format string:
-
a - "am" or "pm"
-
A - "AM" or "PM"
-
d - day of the month, numeric, 2 digits (with leading zeros)
-
D - day of the week, textual, 3 letters; i.e. "Fri"
-
F - month, textual, long; i.e. "January"
-
h - hour, numeric, 12 hour format
-
H - hour, numeric, 24 hour format
-
i - minutes, numeric
-
j - day of the month, numeric, without leading zeros
-
l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"
-
m - month, numeric
-
M - month, textual, 3 letters; i.e. "Jan"
-
s - seconds, numeric
-
S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
-
U - seconds since the epoch
-
Y - year, numeric, 4 digits
-
w - day of the week, numeric, 0 represents Sunday
-
y - year, numeric, 2 digits
-
z - day of the year, numeric; i.e. "299"
Unrecognized characters in the format string will be printed as-is.
Example 1. date example
print(date( "l dS of F Y h:i:s A" ));
print("July 1, 2000 is on a " . date("l", mktime(0,0,0,7,1,2000)));
It is possible to use date and mktime together to find dates in the future or the past.
Example 2. date and mktime example
$tomorrow = mktime(0,0,0,date("m") ,date("d")+1,date("Y"));
$lastmonth = mktime(0,0,0,date("m")-1,date("d"), date("Y"));
$nextyear = mktime(0,0,0,date("m"), date("d", date("Y")+1);
See also gmdate and mktime.
strftime
Name
strftime — format a local time/date according to locale settings
Description
string strftime(string format, 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
-
%d - day of the month as a decimal number (range 0 to 31)
-
%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 1 to 12)
-
%M - minute as a decimal number
-
%p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
-
%S - second as a decimal number
-
%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
-
%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
Example 1. strftime example
setlocale ("LC_TIME", "C"); print(strftime("%A in Finnish is ")); setlocale ("LC_TIME", "fi"); print(strftime("%A, in French ")); setlocale ("LC_TIME", "fr"); print(strftime("%A and in German ")); setlocale ("LC_TIME", "de");
print(strftime("%A.\n"));
This example works if you have the respective locales installed in your system. See also setlocale and mktime.
getdate
Name
getdate — get date/time information
Description
array getdate(int timestamp);
Returns an associative array containing the date information of the timestamp as the following array elements:
-
"seconds" - seconds
-
"minutes" - minutes
-
"hours" - hours
-
"mday" - day of the month
-
"wday" - day of the week, numeric
-
"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"
gmdate
Name
gmdate — format a GMT/CUT date/time
Description
string gmdate(string format, int timestamp);
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".
Example 1. gmdate example
echo date( "M d Y H:i:s",mktime(0,0,0,1,1,1998) );
echo gmdate( "M d Y H:i:s",mktime(0,0,0,1,1,1998) );
See also date, mktime and gmmktime.
mktime
Name
mktime — get UNIX timestamp for a date
Description
int mktime(int hour, int minute, int second, int month, int day, int year);
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.
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".
Example 1. mktime example
echo date( "M-d-Y", mktime(0,0,0,12,32,1997) );
echo date( "M-d-Y", mktime(0,0,0,13,1,1997) );
echo date( "M-d-Y", mktime(0,0,0,1,1,1998) );
See also date and time.
gmmktime
Name
gmmktime — get UNIX timestamp for a GMT date
Description
time
int gmmktime(int hour, int minute, int second, int month, int day, int year);
Identical to mktime except the passed parameters represents a GMT date.
Name
time — return current UNIX timestamp
Description
int time(void);
Returns the current time measured in the number of seconds since the Unix Epoch (January 1, 1970). See also date.
microtime
Name
microtime — return current UNIX timestamp with microseconds
Description
string microtime(void);
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.
See also time.