Here`s an example that shows how easy it is to add or subtract dates and times in PHP. date($format, $timestamp) is one of the most commonly used date and time functions in PHP. It uses the desired output format for the date as the first parameter and an integer as the timestamp value that must be converted to the specified date format. The second parameter is optional, and if omitted, the current date and time are output in string format based on the value $format. The $format parameter accepts a number of characters as valid values. Some of these characters have a simple meaning: Y gives you the complete numerical representation of the year with 4 digits (2018), and y only gives you the last two digits of the current year (18). Similarly, H gives you the time in the 24-hour format with start zeros, but h gives you the time in the 12-hour format with start zeros. Here are some commonly used characters for dates: The following example displays the dates of the next six Saturdays: It is possible to use date() and mktime() together to find data in the future or past. A timestamp is a string that specifies the date and/or time that a particular event occurred. With the characters G and H, you get the current time of day in 24-hour format. You don`t get start zeros with G, but H adds start zeros.
Dates in m/d/y or d-m-y format are disambiguated by looking at the separator between the different components: if the delimiter is a forward slash (/), the American m/d/y is assumed; If the delimiter is a hyphen (-) or a period (.), the European format d-m-y is assumed. However, if the year is specified in a two-digit format and the delimiter is a hyphen (-), the date string is parsed as y-m-d. With relative datetime formats, this function can easily convert frequently used strings to valid timestamp values. The following examples illustrate this: In the following example, the date() function creates a date and time from a set of parameters in the mktime() function: Some characters, such as t, require two backslashes because t is intended for tabs. If possible, it is advisable to simply insert the regular string you want to play outside of date(). Otherwise, you will escape many characters. Here are some of the most common characters in date format and their values. There are four different characters to get the current month in PHP, depending on the format you want. You can use the capital letter F to get the full name of the month like February, or get the month in a shorter three-letter format with M. strtotime — Analyze any English datetime text description in a Unix timestamp The function and method return a DateInterval() object that represents the difference between two dates. This interval can be formatted to produce specific output using all the characters listed in the Format() method documentation. The set time zone is then used by all dates in PHP function scripts.
It has the following syntax. If the number of the year is given in a two-digit format, the values between 00-69 2000-2069 and 70-99 are mapped to 1970-1999. See the following notes for possible differences on 32-bit systems (possible dates may end on 2038-01-19 03:14:07). You`ll often want to work with dates and times when developing websites. For example, you might need to display the date of the last edit in an article or mention how long a reader has been writing a comment. You may also need to view a countdown of days to a special event. Another way to get the timestamp of a specific date would be to use the mktime($hour, $minute, $second, $month, $day $year function. If all parameters are omitted, this function uses only the current local date and time to calculate the timestamp value. This function can also be used with date() to generate useful date and time strings. Procedural style only: DateTime object returned by date_create() The object $date the Datetime class called the getTimestamp() method to convert the date to a Unix timestamp. The Unix timestamp returned by this function does not contain time zone information.
To perform calculations with date/time information, you must use the most powerful DateTimeImmutable. The required format parameter of the date() function specifies how the date (or time) should be formatted. The PHP strtotime() function is used to convert a human-readable date string to a Unix timestamp (the number of seconds since 1. January 1970 00:00:00 GMT). We first created two DateTime objects to store the current time and date of this year`s Christmas. After that, we run for a while to add 1 year to the 2018 Christmas date until the current date is smaller than the Christmas date. This is useful if the code runs on January 18, 2024. The while loop increases the Christmas date as long as it is smaller than the current date at the time this script is run. There are many other special characters to specify the output of the date() function. For more information about special cases, see the table with format characters in the date() function documentation.
Note: The PHP timestamp value is not a constant. This changes every second. If you want the date() function to generate these characters, you must first escape them. Here`s an example: The strtotime($time, [$now =time()]) function is incredibly useful if you want to convert different date and time values in String format to timestamp. .