Function
|
Explanation
|
Example
|
checkdate($month, $date, $year) |
This
function returns true if the values supplied to it make up a valid date. For
example, this function will return false on illegal dates like 31-Feb-05.
Use this
function to validate dates and check that they exist before using them in a
calculation or saving them to a database.
|
<?php
// returns false
echo checkdate(2,30,2005) ? “valid” :
“invalid”;
// returns true
echo checkdate(4,6,2010) ? “valid” :
“invalid”;
?> |
getdate($ts) |
When
used without an argument, this function returns the current date and time as
an associative array. Each element of the array represents a particular
component of the date/time value. An optional timestamp argument can be
passed to the function to obtain date/time values corresponding to that
timestamp.
Use this
function to obtain a series of discrete, easily-separated date/time values.
|
<?php
// get date as associative array
$arr = getdate();
echo “Date is ” . $arr[‘mday’] . ” ” .
$arr[‘weekday’] . ” ” . $arr[‘year’];
echo “Time is ” . $arr[‘hours’] . “:”
. $arr[‘minutes’];
?> |
mktime($hour,
$minute,
$second,
$month, $day,
$year) |
This
function does the opposite of the getdate() function:
it generates a UNIX timestamp (the number of seconds elapsed since Jan 1 1970
00:00 GMT) from a series of date and time values. When used without any
arguments, it generates a UNIX timestamp for the current time instant.
Use this
function to obtain a UNIX timestamp for a time instant. This timestamp format
is commonly used in many databases and programming languages.
|
<?php
// returns timestamp for 13:15:23
7-Jun-2006
echo mktime(13,15,23,6,7,2006);
?> |
date($format,
$ts) |
This
function formats a UNIX timestamp into a human-readable date string. It is
one of the most versatile functions in the PHP date/time API, and can be used
with a wide variety of modifiers to convert integer timestamps into exactly
the string format you require.
Use this
function when formatting dates and times for display.
|
<?php
// format current date
// returns “13-Sep-2005 01:16 PM”
echo date(“d-M-Y h:i A”, mktime());
?> |
strtotime($str) |
This
function converts a human-readable English date/time string into a UNIX
timestamp.
Use this
function to convert non-standard date/time strings into standard, easily
portable UNIX timestamps.
|
<?php
// returns 13-Sep-05
echo date(“d-M-y”, strtotime(“today”));
// returns 14-Sep-05
echo date(“d-M-y”, strtotime(“tomorrow”));
// returns 16-Sep-05
echo date(“d-M-y”, strtotime(“today +3
days”));
?> |
strftime($format, $ts) |
This
function formats a UNIX timestamp into a date string suitable for the current
locale, as previously defined with setlocale().
Use this
function to create date strings compliant with the current locale settings.
|
<?php
// set locale to France (on Windows) setlocale(LC_TIME, “fra_fra”);
// format month/day names
// as per locale setting
// returns “septembre” and “mardi”
echo strftime(“Month: %B “);
echo strftime(“Day: %A “);
?> |
microtime() |
This
function returns the number of seconds and microseconds elapsed between the
current time and Jan 1 1970 00:00 GMT.
Use this
function when benchmarking a particular code block, to more precisely measure
how long it takes to execute.
|
<?php
// get starting value
$start = microtime();
// run some code
for ($x=0; $x<1000; $x++) { $null = $x * $x;
}
// get ending value
$end = microtime();
// calculate time taken for code
execution
echo “Elapsed time: ” . ($end –
$start) .” sec”;
?> |
gmmktime($hour,
$minute,
$second,
$month, $day,
$year) |
This
function generates a UNIX timestamp from a series of date and time values
expressed in GMT. When used without any arguments, it generates a UNIX
timestamp for the current GMT time instant.
Use this
function to obtain a UNIX timestamp for a GMT time instant.
|
<?php
// returns timestamp for 12:25:23
9-Jul-2006
echo gmmktime(12,25,23,7,9,2006);
?> |
gmdate($format,
$ts) |
This
function formats a UNIX timestamp into a human-readable date string. This
date string is expressed in GMT (not local time).
Use this
function when representing a timestamp in GMT.
|
<?php
// format current date into GMT
// returns “13-Sep-2005 08:32 AM”
echo gmdate(“d-M-Y h:i A”, mktime());
?> |
date_default_ timezone_set
($tz) and date_default_ timezone_get() |
This
function sets and retrieves the default time zone for all subsequent
date/time function calls.
Note: This
function is only available in PHP 5.1+
Use this
function as a convenient shortcut to set a time zone for subsequent date
operations.
|
<?php
// set timezone to UTC date_default_timezone_set(‘UTC’);
?> |