Home >Backend Development >PHP Problem >Detailed explanation of how PHP processes dates and converts them into various formats
In PHP applications, date and time are one of the most commonly used data types. In many applications, dates need to be presented to the user in human-readable form. Fortunately, PHP provides some built-in functions to handle dates and times and convert them into various formats.
In this article, we will learn how to convert dates to various date formats using PHP. We will learn in depth some important functions and how they are used in PHP code.
First let us take a look at the date()
function. It is one of the most basic date formatting functions in PHP. date()
The function starts with two required parameters. The first parameter is a format string that defines the format of the output date. The second parameter is a timestamp representing the date and time to be formatted. If no second parameter is provided, date()
will return the current date and time.
Here is a simple example, using the default date and time format:
echo date('Y-m-d H:i:s');
This will output the current date and time in the format: Year-Month-Day Hour:Minute: Second
.
Now let’s look at some common date formatting options:
Tags | Description | Example |
---|---|---|
Y | Four-digit year | 2021 |
m | Month, with leading zeros | 01-12 |
d | Day of the month, with leading zeros | 01-31 |
H | The hour in 24-hour format, with leading zeros | 00-23 |
i | Minutes, with leading zeros | 00-59 |
s | Seconds, with leading zeros Zero | 00-59 |
l | Full text of the day of the week (lowercase L) | Sunday-Saturday |
D | Abbreviated text for the day of the week (three characters) | Sun-Sat |
M | Abbreviated text of month (three characters) | Jan-Dec |
F | Full text of month | January -December |
The day of the month without leading zeros | 1-31 | |
Month, no leading zeros | 1-12 | |
Two-digit year | 21 | |
Lowercase morning or afternoon (am or pm) | am/pm | |
Capital morning or afternoon (AM or PM) | AM/PM | |
given month Number of days | 28-31 |
echo date('Y-m-d'); // 2021-08-29 echo date('F j, Y, g:i a'); // August 29, 2021, 3:12 pm echo date('l, F jS Y'); // Sunday, August 29th 2021strtotime() function
strtotime() function is another important function in PHP for handling date and time. It converts any string into a timestamp. This string can contain a date and time, or it can contain relative time expressions, such as "yesterday", "one month ago", "4pm", etc.
strtotime() to convert a string to a timestamp:
echo strtotime('now'); // 1630204755 echo strtotime('2021-08-29'); // 1630204800 echo strtotime('next Tuesday'); // 1630492800As you can see,
strtotime() Can accept many different types of string expressions and convert them to timestamps. This makes working with dates and times very flexible.
$now = new DateTime(); echo $now->format('Y-m-d H:i:s'); // 2021-08-29 16:12:35 $date = new DateTime('2021-08-29'); echo $date->format('F j, Y'); // August 29, 2021 $date->modify('+1 month'); echo $date->format('F j, Y'); // September 29, 2021As you can see, the DateTime class provides some methods for working with dates and times. We can use the
format() method to format the date into any format, and we can use the
modify() method to add or subtract the date and time. In addition, the DateTime class has many other useful methods and properties, such as the
diff() method, which is used to calculate the difference between two dates.
The above is the detailed content of Detailed explanation of how PHP processes dates and converts them into various formats. For more information, please follow other related articles on the PHP Chinese website!