You are on page 1of 5

Dates

Getting the Date with time()


PHP's time() function gives you all the information you need about the current date
and time. It requires no arguments but returns an integer. This number is a little
hard on the eyes, for us humans, but extremely useful nonetheless:

date_default_timezone_set("asia/hongkong")
print time();

Converting a Timestamp with getdate()


Now that you have a timestamp to work with, you must convert it before you present
it to the user. getdate() optionally accepts a timestamp and returns an associative
array containing information about the date. If you omit the timestamp, it works
with the current timestamp as returned by time().

<?php
$date_array = getdate(); // no argument passed so today's date will be used
foreach ( $date_array as $key => $val ) {
print "$key = $val<br/>";
}
?>
<hr/>
<p>
<?
print "Today's date: ";
print $date_array['mon']."/".$date_array['mday']."/".$date_array['year'];
?>

Converting a Timestamp with date()


The date() function returns a formatted string representing a date. You can
exercise an enormous amount of control over the format that date() returns with a
string argument you must pass to it. In addition to the format string, date()
optionally accepts a timestamp.

Format Codes for Use with date()

Format

Description

Example

am or pm lowercase

pm

AM or PM uppercase

PM

Swatch beat (timezone-free 'Internet time')


771

Day of month (number with leading zeroes)

08

Day of week (three letters)

Wed

Month name

October

Hour (12-hour format�no leading zeroes)

Hour (24-hour format�no leading zeroes)

18

Hour (12-hour format�leading zeroes)

06

Hour (24-hour format�leading zeroes)

18

Minutes

31

Daylight savings time (Boolean value)

j
Day of the month (no leading zeroes)

Day of the week (name)

Wednesday

Leap year (1 for yes, 0 for no)

Month of year (number�leading zeroes)

10

Month of year (three letters)

Oct

Month of year (number�no leading zeroes)

10

Offset in hours from GMT (in [+-]HHMM format)

+0100

Full date standardized to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)

Wed, 8 Oct 2003 18:31:15+0100

Seconds, with leading zeroes

15

English suffix for date in month (e.g. 20th)

th

t
Number of days in the given month

31

Timezone setting on the machine used

BST

Unix timestamp

1065634275

Day of week (number indexed from Sunday = 0)

Week of year

41

Year (two digits)

03

Year (four digits)

2003

Day of year (0�366)

280

Offset in seconds from GMT

3600

<?php
print date("m/d/y G.i:s", time());
// 10/08/03 19.17:42
print "<br/>";
print "Today is ";
print date("jS of F Y, \a\\t g.i a", time());
// Today is 8th of October 2003, at 7.17 pm
?>

Creating Timestamps with mktime()


You can already get information about the current time, but you cannot yet work
with arbitrary dates. mktime() returns a timestamp you can then use with date() or
getdate(). mktime() accepts up to six integer arguments in the following order:

hour

minute

second

month

day of month

year

<?php
// make a timestamp for 1/5/04 at 2.30 am
$ts = mktime( 2, 30, 0, 5, 1, 2004 );
print date("m/d/y G.i:s", $ts);
// 05/01/04 2.30:00
print "<br/>";
print "The date is ";
print date("jS of F Y, \a\\t g.i a", $ts );
// The date is 1st of May 2004, at 2.30 am
?>

Testing a Date with checkdate()


You might need to accept date information from user input. Before you work with
this date or store it in a database, you should check that the date is valid.
checkdate() accepts three integers: month, day, and year. checkdate() returns true
if the month is between 1 and 12, the day is acceptable for the given month and
year (accounting for leap years), and the year is between 0 and 32767. Be careful,
though, because a date might be valid but not acceptable to other date functions.
For example, the following line returns true:

checkdate( 4, 4, 1066 )

You might also like