PHP date and time can be formatted in multiple different ways in your PHP code. You might need a Unix timestamp or GMT date, a DateTime object or an ISO date. Therefore, it is important to learn functions that are applied to manipulate PHP DateTime format.
PHP date formatting is not that hard, but you still have to have specific knowledge and expertise. Fetching the date and time from the server you are using is only the first step. Don't let a wrong time zone or PHP date format throw you off your feet!
Contents
PHP DateTime Format: Main Tips
- Time and date PHP functions allow you to get date and time from the server you're using.
- These functions are inbuilt into the PHP core.
- Keep in mind that these functions are dependant on your server's locale setting.
Runtime Configuration
When using these functions to set PHP DateTime format, remember that how they behave will be affected by the settings located in php.ini:
Name | Description | Default | PHP Version |
---|---|---|---|
date.timezone | Default timezone (is used by all PHP date/time functions) | "" | PHP 5.1 and newer |
date.default_latitude | Default latitude (is used by date_sunrise() and date_sunset()) | "31.7667" | PHP 5.0 and newer |
date.default_longitude | Default longitude (is used by date_sunrise() and date_sunset()) | "35.2333" | PHP 5.0 and newer |
date.sunrise_zenith | Default sunrise zenith (is used by date_sunrise() and date_sunset()) | "90.83" | PHP 5.0 and newer |
date.sunset_zenith | Default sunset zenith (is used by date_sunrise() and date_sunset()) | "90.83" | PHP 5.0 and up |
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Time And Date Functions
Analyze the table below. It is a handy guide for the inbuilt functions that can be used for all your needs when it comes to setting the PHP DateTime format. Feel free to return to it whenever you need a reminder.
Function | Description |
---|---|
checkdate() | Validate Gregorian date |
date_add() | Add days, months, years, hours, minutes, and seconds to date |
date_create_from_format() | Return new DateTime object formatted according to specified format |
date_create() | Return new DateTime object |
date_date_set() | Set new date |
date_default_timezone_get() | Return default timezone used by all PHP date/time functions |
date_default_timezone_set() | Set default timezone used by all PHP date/time functions |
date_diff() | Return difference between two PHP dates |
date_format() | Return PHP date formatted according to specified format |
date_get_last_errors() | Return warnings/errors found in PHP date string |
date_interval_create_from_date_string() | Set up PHP DateInterval from relative parts of string |
date_interval_format() | Format interval |
date_isodate_set() | Set ISO date |
date_modify() | Modify PHP timestamp |
date_offset_get() | Return timezone offset |
date_parse_from_format() | Return associative array with detailed info about specified date, according to specified format |
date_parse() | Return associative array with detailed info about specified date |
date_sub() | Subtract days, months, years, hours, minutes, and seconds from date |
date_sun_info() | Return array containing info about sunset/sunrise and twilight begin/end, for specified day and location |
date_sunrise() | Return sunrise time for specified day and location |
date_sunset() | Return sunset time for specified day and location |
date_time_set() | Set PHP time |
date_timestamp_get() | Return Unix PHP timestamp |
date_timestamp_set() | Set date and time based on Unix PHP timestamp |
date_timezone_get() | Return time zone of given DateTime object |
date_timezone_set() | Set time zone for DateTime object |
date() | Format local date and time |
getdate() | Return date/time information of PHP timestamp or current local date/time |
gettimeofday() | Return current time |
gmdate() | Format GMT/UTC date and time |
gmmktime() | Return Unix PHP timestamp for GMT date |
gmstrftime() | Format GMT/UTC date and time according to locale settings |
idate() | Format local time/date as int |
localtime() | Return local time |
microtime() | Return currently available Unix PHP timestamp in microseconds |
mktime() | Return Unix PHP timestamp for date |
strftime() | Format local time and/or date according to locale settings |
strptime() | Parse time/date generated with strftime() |
strtotime() | Parse English textual datetime into Unix PHP timestamp |
time() | Return current time as Unix PHP timestamp |
timezone_abbreviations_list() | Return associative array containing dst, offset, and timezone name |
timezone_identifiers_list() | Return indexed array with all timezone identifiers |
timezone_location_get() | Return location information for a specified timezone |
timezone_name_from_ abbr() | Returns the timezone name from abbreviation |
timezone_name_get() | Return name of timezone |
timezone_offset_get() | Return timezone offset from GMT |
timezone_open() | Create new DateTimeZone object |
timezone_transitions_get() | Return all transitions for timezone |
timezone_version_get() | Return version of timezone db |
List of Predefined Constants
There are also a few predefined constants that can be used in PHP date formatting. Take a look at their names and descriptions:
Constant | Description |
---|---|
DATE_ATOM | Atom (i.e.: 2005-08-17T16:15:03+0000) |
DATE_COOKIE | HTTP Cookies (i.e.: Sun, 18 Aug 2005 14:13:03 UTC) |
DATE_ISO8601 | ISO-8601 (i.e.: 2005-08-14T17:13:03+0000) |
DATE_RFC822 | RFC 822 (i.e.: Sun, 14 Aug 2006 16:12:03 UTC) |
DATE_RFC850 | RFC 850 (i.e.: Sunday, 14-Aug-06 16:13:03 UTC) |
DATE_RFC1036 | RFC 1036 (i.e.: Sunday, 14-Aug-04 16:13:03 UTC) |
DATE_RFC1123 | RFC 1123 (i.e.: Sun, 14 Aug 2006 16:13:03 UTC) |
DATE_RFC2822 | RFC 2822 (i.e.:Sun, 14 Aug 2006 16:13:03 +0000) |
DATE_RSS | RSS (i.e.:Sun, 14 Aug 2006 17:15:04 UTC) |
DATE_W3C | World Wide Web Consortium (i.e.: 2007-09-14T16:11:05+0000) |
PHP DateTime Format: Summary
- To get dates and time, inbuilt PHP time and date functions and constants are used.
- These functions are related to the locale settings of the server you're using.
- Always keep time inconsistencies (such as daylight savings, time zones and leap years) in mind when working with PHP date functions.