COM372: Notes, Chapter 20

Managing the Date and Time
pages 439-449

Syllabus | Grading | Reading Notes | Dr. Logan

The Date () function: This function takes a format string and an optional Unix timestamp (default is current date and time) and returns a formatted string. Format codes are listed in table 20.1. Instead, here is the table from the php manual:

Table 42. The following characters are recognized in the format parameter string

format characterDescriptionExample returned values
Day------
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
l (lowercase 'L')A full textual representation of the day of the weekSunday through Saturday
NISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
Week------
WISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)Example: 42 (the 42nd week in the year)
Month------
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
Year------
LWhether it's a leap year1 if it is a leap year, 0 otherwise.
oISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)Examples: 1999 or 2003
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
Time------
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
BSwatch Internet time000 through 999
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59
Timezone------
eTimezone identifier (added in PHP 5.1.0)Examples: UTC, GMT, Atlantic/Azores
I (capital i)Whether or not the date is in daylight saving time1 if Daylight Saving Time, 0 otherwise.
ODifference to Greenwich time (GMT) in hoursExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)Example: +02:00
TTimezone abbreviationExamples: EST, MDT ...
ZTimezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.-43200 through 50400
Full Date/Time------
cISO 8601 date (added in PHP 5)2004-02-12T15:19:21+00:00
r» RFC 2822 formatted dateExample: Thu, 21 Dec 2000 16:01:07 +0200
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)See also time()

To create a time stamp from a specific date, use mktime(), with syntax int mktime([int hour[, int minute[, int second[, int month[, int day[, int year[, int is_dst]]]]]]). To get current date and time, use time(). The date() function as date("U") also returns the current time.

Getdate(): Used as array detdate([int timestamp]), this returns an array with the parts of the date and time, with keys seconds, minutes, hours, mday (day of the month, numeric), wday (day of the week, numeric), mon, year, yday (day of the year, numeric), weekeday (day of the week, full-text), and 0 (timestamp, numeric).

Validating dates: Checkdate() uses systax int checkdate (int month, int day, int year). It checks whether the year is an integer between 0 and 32,767, month is between 1 and 12, and whether day exists in that month (e.g., checkdate(2,30, 2001) returns false.

PHP Vs. MySQL Date Formats

Converting: MySQL uses ISO 8601 format, requiring dates to have the year first (Sept. 11, 2001 is 2001-09-11 or 01-09-11). To convert PHP dates into MySQL, use date(), as above. To convert dates within MySQL, use date_format() or unix_timestamp(). Date_Format()uses format codes (table 20.3, p. 444), %M (month, full text), %W (weekday, full text), %D (day of month, numeric with text suffix...2nd), %Y (year, numeric, 4 digits), %y (year, numeric, 2 digits), %a (weekday name, 3 characters), %d (day of month, numeric, leading zeros), %e (day of month, numeric, no leading zeros), %m (month, numeric, leading zeros), %c (month, numeric, no leading zeros), %b (month, text, 3 characters), %j (day of year, numeric), %H (hour, 24-hour clock, leading zeros), %k (Hour, 24-hour clock, no leading zeros), %h or %I (hour, 12-hour clock, leading zeros), %l (hour, 12-hour clock, no leading zeros), %i (minutes, numeric, leading zeros), %r (time, 12-hour (hh:mm:ss [AM | PM]), %T (Time, 24-hour (hh:mm:ss), %S or %s (seconds, numeric, leading zeros), %p (AM or PM), and %w (day of week, numeric, from 0 (sunday) to 6 (Saturday))

Calculating dates: Length of time between dates uses the difference between Unix timestamps (listing 20.1, p. 445-6). To calculate the days since "Mission Accomplished" (May 2, 2003, 9 pm), use
<php
/* mission accomplished speech, 9 pm, May 2, 2003 */
$day = 2;
$month = 5;
$year = 2003;
$mday_unix=mktime (0,0,0,$month, $day, $year);
$today_unix=time();$diff=$today_unix-$mday_unix;
$days_since=floor($diff / (365*24*60*60));
echo "<p>It is now ".$days_since." days since Mission Accomplished/.</p>
?>

MySQL date trick: You can use MySQL date functions to do date manipulation, as described on p. 446-7; one advantage is that the MySQL date functions are not limited to after 1-1-1970 as PHP dates are.

Microtime: The PHP function microtime() returns a timestamp suitable to measure very short time periods. (more)