Skip Navigation Links
EZWEB
PHPExpand PHP

Dates and Times

date( )

The date( ) function takes two parameters, the second is optional.

  • format string, instructing PHP to display the date in a specific format
  • timestamp, indicating to PHP the date we are to format – if no timestamp is provided, PHP formats the current date

PHP includes many formatting codes that return the date in a specific format. Any book on PHP should have a listing, or go to the PHP manual to obtain the entire list.

         $today = date( “jS F Y” );

returns the current date in the following format:

         10th September 2003

The string format can contain more than just the format codes.

         echo date( ‘m/d/Y’ );

prints the following:

         10/31/2003

You may need to escape certain letters to print them as literals rather than the format code:

         echo( ‘\h: h, \m: m, \s: s’ );

prints the following:

         h: 07, m: 13, s: 47

  • The second, optional parameter is a UNIX timestamp.
  • Most UNIX systems store the date and time information in a unique format called a timestamp.
  • It’s a 32 bit integer containing the number of seconds since midnight, 1 January, 1970, known as the UNIX Epoch.
  • This may seem odd if you are not familiar with UNIX, but it is a standard.
  • Because it is a 32 bit integer, it can hold  up to 4,294,967,296 seconds.
  • UNIX timestamps did not suffer Y2K problems, but it will face similar problems in 2038 when the 4billion+ seconds run out. That is assuming we are still using the same software.

UNIX timestamp turned 1 billion on 9/11/2001.

If no timestamp is provided as a second parameter to date( ), PHP uses the current date and time.

mktime( )

If you want to convert a date and time to a UNIX timestamp, use the mktime( ) function. This function takes 6 arguments:

Syntax:

mktime( hour, minute, second, month, day, year );

Example:

mktime( 0, 0, 1, 1, 1, 1970 );

will return a value of 1

  • A common programming error is to mix up the order of the arguments passed to mktime( ), which differs from the order of arguments in a regular UNIX mktime( ) call.
  • Arguments can be left out in order from right to left.
  • If you leave out an argument PHP will default to the current date or time.
  • If you leave out arguments from the left side of the list, you will end up with errors.
  • If you don’t care about time values, pass in zeros as the arguments.

Example:

$timestamp = mktime( );

returns the timestamp for the current date and time.

  • Negative timestamps are not supported under any version of Windows, limiting the range of years from 1970 – 2038. Otherwise the valid range of years is from 1901 – 2038.


getdate( )

  • Another function to work with dates and times is getdate( ).
  • It takes a timestamp as an optional parameter.
  • If no parameter is provided, it returns information on the current date and time.
  • getdate( ) returns an associative array with the following keys and values:

Key

Description

Example returned values

"seconds"

Numeric representation of seconds

0 to 59

"minutes"

Numeric representation of minutes

0 to 59

"hours"

Numeric representation of hours

0 to 23

"mday"

Numeric representation of the day of the month

1 to 31

"wday"

Numeric representation of the day of the week

0 (Sunday) through 6 (Saturday)

"mon"

Numeric representation of a month

1 through 12

"year"

A full numeric representation of a year, 4 digits

Examples: 1999 or 2003

"yday"

Numeric representation of the day of the year

0 through 356

"weekday"

A full textual representation of the day of the week

Sunday through Saturday

"month"

A full textual representation of a month, such as January or March

January through December

0

Seconds since the Unix Epoch

System Dependent, typically -2147483648
through 2147483647.

         $today = getdate(); 
         print_r($today);

will print;

Array
(
    [seconds] => 40
    [minutes] => 58
    [hours]   => 21
    [mday]    => 17
    [wday]    => 2
    [mon]     => 6
    [year]    => 2003
    [yday]    => 167
    [weekday] => Tuesday
    [month]   => June
    [0]       => 1055901520
)


 checkdate( )

checkdate( ) checks whether a date is valid.  It is a useful function to validate user input.

          checkdate( month, day, year );
         checkdate( 1, 24, 2000 );  //  returns TRUE
         checkdate( 2, 30, 2000 );  //  returns FALSE

checkdate( ) will take leap year into consideration

date and time calculations

One of the easiest ways to compare 2 dates is to compare their timestamps – if one is less than another, it occurred first.

To calculate the length of time between 2 dates, take the difference between the timestamps.





Free Tutorials and Training