# Time and Date PHP

All the time functions work off the UNIX epoch, which is January 1, 1970. Dates are expressed as seconds since the epoch. This makes it easy to refer to dates with integers. When a function calls for seconds since the epoch, I've referred to it as a timestamp.

boolean checkdate(integer month, integer day, integer year)
The checkdate function returns TRUE if a date is valid, FALSE otherwise. A day is considered valid if the year is between 0 and 32,767, the month is between 1 and 12, and lastly, if the day is within the allowable days for that month.

<?
if(checkdate(2,18,1970))
{
print("It is a good day");
}
?>

string date(string format, integer timestamp)
The date function returns a string describing the date of the timestamp according to the format argument. Letters in the format argument are replaced with parts of the date or time. Any characters not understood as codes are passed along in place.The timestamp argument is optional. If left out, the current time will be used. The timestamp is interpreted as being in local time.

<?
//prints something like
//03:59 PM Monday January 1st, 2001
print(date("h:i A l F dS, Y"));
?>

array getdate(integer timestamp)
The getdate function returns an associative array with information about the given date. The timestamp argument is the number of seconds since January 1, 1970. If left out, the current time is used.

getdate

array gettimeofday()

The gettimeofday function returns an associative array containing information about the current time. This is a direct interface to the C function of the same name.

gettimeofday

string gmdate(string format, integer timestamp)

The gmdate function operates identically to the date function except that, rather than return the time for the local time zone, Greenwich Mean Time is returned.

<?
print("Local: ");
print(date("h:i A l "));
print(date("F dS, Y"));
print("<BR>n");
print("GMT: ");
print(gmdate("h:i A l "));
print(gmdate("F dS, Y"));
print("<BR>n");
?>

string gmstrftime(string format, integer timestamp)

The gmstrftime function operates identically to strftime except that the timestamp is translated into Greenwich Mean Time. The same format codes defined are used in the format argument.

<?
print(gmstrftime("%A, %c", mktime(0, 0, 0, 1, 1,
1970)));
?>

string microtime()
The microtime function returns a string with two numbers separated by a space. The first number is microseconds on the system clock. The second is the number of seconds since January 1, 1970.

<?
//print microtime
print("Start: ". microtime() . "<BR>n");
//sleep for a random time
usleep(rand(100,5000));
//print microtime
print("Stop: " . microtime() . "<BR>n");
?>

integer mktime(integer hour, integer minute, integer second,integer month, integer day, integer year)
The mktime function returns a timestamp for a given date, the number of seconds since January 1, 1970. All the arguments are optional and, if left out, the appropriate value for the current time will be used. If an argument is out of range, mktime will account for the surplus or deficit by modifying the other time units. For example, using 13 for the month argument is equivalent to January of the following year. This makes mktime an effective tool for adding arbitrary time to a date.

<?
print("Fifty Hours from Now: ");
print(date("h:i A l F dS, Y", mktime(date("h")+50)));
print("<BR>n");
?>

string strftime(string format, integer timestamp)

The strftime function returns a date in a particular format. If the optional timestamp argument is left out, the current time will be used. Language-dependent strings will be set according to the current locale, which may be changed with the setlocale function. The format string may contain codes that have special meaning and begin with a percentage sign. Other characters are passed through unchanged.

<?
//prints something like
//Monday, 01/01/01 16:04:12
print(strftime("%A, %c"));
?>

integer strtotime(string date, integer now)

The strtotime function attempts to parse a string containing date and time, returning the timestamp for it. If partial information is provided in the date argument, the missing information will be drawn from the now argument. You may leave out the now argument to use the current time.

<?
//create a reason description
//of a date
$time = "Feb 18, 1970 3AM"; //get its timestamp$ts = strtotime($time); //print it to verify that it worked print(date("h:i A l F dS, Y",$ts));
?>

integer time()
Use time to get the current timestamp.

<?
print(time());
?>

0