COM372: Notes, Chapter 20
Managing the Date and Time
pages 439-449
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 character | Description | Example returned values |
|---|---|---|
| Day | --- | --- |
| d | Day of the month, 2 digits with leading zeros | 01 to 31 |
| D | A textual representation of a day, three letters | Mon through Sun |
| j | Day of the month without leading zeros | 1 to 31 |
| l (lowercase 'L') | A full textual representation of the day of the week | Sunday through Saturday |
| N | ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) | 1 (for Monday) through 7 (for Sunday) |
| S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd or th. Works well with j |
| w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
| z | The day of the year (starting from 0) | 0 through 365 |
| Week | --- | --- |
| W | ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) | Example: 42 (the 42nd week in the year) |
| Month | --- | --- |
| F | A full textual representation of a month, such as January or March | January through December |
| m | Numeric representation of a month, with leading zeros | 01 through 12 |
| M | A short textual representation of a month, three letters | Jan through Dec |
| n | Numeric representation of a month, without leading zeros | 1 through 12 |
| t | Number of days in the given month | 28 through 31 |
| Year | --- | --- |
| L | Whether it's a leap year | 1 if it is a leap year, 0 otherwise. |
| o | ISO-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 |
| Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
| y | A two digit representation of a year | Examples: 99 or 03 |
| Time | --- | --- |
| a | Lowercase Ante meridiem and Post meridiem | am or pm |
| A | Uppercase Ante meridiem and Post meridiem | AM or PM |
| B | Swatch Internet time | 000 through 999 |
| g | 12-hour format of an hour without leading zeros | 1 through 12 |
| G | 24-hour format of an hour without leading zeros | 0 through 23 |
| h | 12-hour format of an hour with leading zeros | 01 through 12 |
| H | 24-hour format of an hour with leading zeros | 00 through 23 |
| i | Minutes with leading zeros | 00 to 59 |
| s | Seconds, with leading zeros | 00 through 59 |
| Timezone | --- | --- |
| e | Timezone identifier (added in PHP 5.1.0) | Examples: UTC, GMT, Atlantic/Azores |
| I (capital i) | Whether or not the date is in daylight saving time | 1 if Daylight Saving Time, 0 otherwise. |
| O | Difference to Greenwich time (GMT) in hours | Example: +0200 |
| P | Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) | Example: +02:00 |
| T | Timezone abbreviation | Examples: EST, MDT ... |
| Z | Timezone 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 | --- | --- |
| c | ISO 8601 date (added in PHP 5) | 2004-02-12T15:19:21+00:00 |
| r | » RFC 2822 formatted date | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
| U | Seconds 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)