PHP Date() FunctionThe PHP date() function is used to format a time and/or date. While PHP's date() function may seem to have an overwhelming amount of options available, isn't it always better to have more choices than not enough? With PHP's date function you format timestamps, so they are more human readable. Note: A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. The date function always formats a timestamp, whether you supply one or not. - What's a timestamp? Good question!
Timestamp: A timestamp is the number of seconds from January 1, 1970 at 00:00. Otherwise known as the Unix Timestamp, this measurement is a widely used standard that PHP has chosen to utilize. Syntax: date(format,timestamp) | Parameter | Description | | format | Required. Specifies the format of the timestamp | | timestamp | Optional. Specifies a timestamp. Default is the current date and time | PHP Date() - Format the DateThe required format parameter in the date() function specifies how to format the date/time. The date function uses letters of the alphabet to represent various parts of a typical date and time format. Here are some characters that can be used: - d - Represents the day of the month (01 to 31)
- m - Represents a month (01 to 12)
- Y - Represents a year (in four digits)
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting: Code: <?php echo date("Y/m/d") . "<br />"; echo date("Y.m.d") . "<br />"; echo date("Y-m-d") ?> An output will be: 2009/05/11 2009.05.11 2009-05-11 Be sure to test this out on your own PHP enabled server, it's really great to see the instant results available with PHP date! PHP Date() - Adding a Timestamp The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used. The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified. Syntax for mktime() : mktime(hour,minute,second,month,day,year,is_dst) To go one day in the future we simply add one to the day argument of mktime(): Code: <?php $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); echo "Tomorrow is ".date("Y/m/d", $tomorrow); ?> An output will be: Tomorrow is 2009/05/12 The date/time functions allow you to extract and format the date and time on the server. Note: These functions depend on the locale settings of the server! Runtime Configuration The behavior of the date/time functions is affected by settings in php.ini. Date/Time configuration options: | Name | Default | Description | Changeable | | date.default_latitude | "31.7667" | Specifies the default latitude (available since PHP 5). This option is used by date_sunrise() and date_sunset() | PHP_INI_ALL | | date.default_longitude | "35.2333" | Specifies the default longitude (available since PHP 5). This option is used by date_sunrise() and date_sunset() | PHP_INI_ALL | | date.sunrise_zenith | "90.83" | Specifies the default sunrise zenith (available since PHP 5). This option is used by date_sunrise() and date_sunset() | PHP_INI_ALL | | date.sunset_zenith | "90.83" | Specifies the default sunset zenith (available since PHP 5). This option is used by date_sunrise() and date_sunset() | PHP_INI_ALL | | date.timezone | "" | Specifies the default timezone (available since PHP 5.1) | PHP_INI_ALL |
PHP Date / Time Functions PHP: indicates the earliest version of PHP that supports the function. | Function | Description | PHP | | checkdate() | Validates a Gregorian date | 3 | | date_default_timezone_get() | Returns the default time zone | 5 | | date_default_timezone_set() | Sets the default time zone | 5 | | date_sunrise() | Returns the time of sunrise for a given day / location | 5 | | date_sunset() | Returns the time of sunset for a given day / location | 5 | | date() | Formats a local time/date | 3 | | getdate() | Returns an array that contains date and time information for a Unix timestamp | 3 | | gettimeofday() | Returns an array that contains current time information | 3 | | gmdate() | Formats a GMT/UTC date/time | 3 | | gmmktime() | Returns the Unix timestamp for a GMT date | 3 | | gmstrftime() | Formats a GMT/UTC time/date according to locale settings | 3 | | idate() | Formats a local time/date as integer | 5 | | localtime() | Returns an array that contains the time components of a Unix timestamp | 4 | | microtime() | Returns the microseconds for the current time | 3 | | mktime() | Returns the Unix timestamp for a date | 3 | | strftime() | Formats a local time/date according to locale settings | 3 | | strptime() | Parses a time/date generated with strftime() | 5 | | strtotime() | Parses an English textual date or time into a Unix timestamp | 3 | | time() | Returns the current time as a Unix timestamp | 3 | PHP Date / Time Constants PHP: indicates the earliest version of PHP that supports the constant. | Constant | Description | PHP | | DATE_ATOM | Atom (example: 2005-08-15T16:13:03+0000) | | | DATE_COOKIE | HTTP Cookies (example: Sun, 14 Aug 2005 16:13:03 UTC) | | | DATE_ISO8601 | ISO-8601 (example: 2005-08-14T16:13:03+0000) | | | DATE_RFC822 | RFC 822 (example: Sun, 14 Aug 2005 16:13:03 UTC) | | | DATE_RFC850 | RFC 850 (example: Sunday, 14-Aug-05 16:13:03 UTC) | | | DATE_RFC1036 | RFC 1036 (example: Sunday, 14-Aug-05 16:13:03 UTC) | | | DATE_RFC1123 | RFC 1123 (example: Sun, 14 Aug 2005 16:13:03 UTC) | | | DATE_RFC2822 | RFC 2822 (Sun, 14 Aug 2005 16:13:03 +0000) | | | DATE_RSS | RSS (Sun, 14 Aug 2005 16:13:03 UTC) | | Important Full Date and Time: - r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")
Time: - a: am or pm depending on the time
- A: AM or PM depending on the time
- g: Hour without leading zeroes. Values are 1 through 12.
- G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
- h: Hour with leading zeroes. Values 01 through 12.
- H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
- i: Minute with leading zeroes. Values 00 through 59.
- s: Seconds with leading zeroes. Values 00 through 59.
Day: - d: Day of the month with leading zeroes. Values are 01 through 31.
- j: Day of the month without leading zeroes. Values 1 through 31
- D: Day of the week abbreviations. Sun through Sat
- l: Day of the week. Values Sunday through Saturday
- w: Day of the week without leading zeroes. Values 0 through 6.
- z: Day of the year without leading zeroes. Values 0 through 365.
Month: - m: Month number with leading zeroes. Values 01 through 12
- n: Month number without leading zeroes. Values 1 through 12
- M: Abbreviation for the month. Values Jan through Dec
- F: Normal month representation. Values January through December.
- t: The number of days in the month. Values 28 through 31.
Year: - L: 1 if it's a leap year and 0 if it isn't.
- Y: A four digit year format
- y: A two digit year format. Values 00 through 99.
Other Formatting: - U: The number of seconds since the Unix Epoch (January 1, 1970)
- O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
We suggest that you take a few minutes to create several timestamps using PHP's mktime function and just try out all these different letters to get your feet wet with PHP's date function.
|