In PHP development, date and time operations are an essential part. Whether it is processing user registration time or recording article publishing time, date and time need to be processed. This article will introduce in detail how to operate date and time in PHP, hoping to help PHP developers.
1. Timestamp
Time stamp is a method of expressing time in seconds, usually from January 1, 1970 00:00:00 UTC to the current time Number of seconds. In PHP, you can use the time() function to get the current timestamp:
echo time();
Output:
1602603419
It should be noted that the time() function returns the timestamp in the time zone of the current server. . If you need to obtain the timestamp in a specified time zone, you can use a DateTime object for conversion, which will be introduced later.
2. Date formatting
For timestamps, we can use the date() function to format them into the date string we need. The first parameter of the date() function is the date format, which can contain various placeholders to represent different date and time elements. Here are some of the most commonly used placeholders:
Placeholder | Description |
---|---|
Y | Year, four digits |
m | Month, two digits |
d | Date, two digits |
H | Hour, two digits, 24-hour format |
i | Minutes, two digits |
#s | Seconds, two digits |
For example, convert the timestamp to year, month and day format:
echo date('Y-m-d', time());
Output:
2020-10-13
Convert the timestamp to date time format with hours, minutes and seconds:
echo date('Y-m-d H:i:s', time());
Output:
2020-10-13 09:43:39
3. Date calculation
During development, sometimes it is necessary to add or subtract dates, such as obtaining the date 10 days later. PHP provides the DateTime class, which can easily perform date calculations.
First you need to create a DateTime object. You can pass in a date string in the constructor, or use the createFromFormat() method to create an object according to the specified format:
$dateTime = new DateTime('2020-10-13'); // 或者 $dateTime = DateTime::createFromFormat('Y-m-d', '2020-10-13');
Then you can use add () method and sub() method perform addition and subtraction operations. For example, get the date 10 days later:
$dateTime->add(new DateInterval('P10D')); echo $dateTime->format('Y-m-d');
Output:
2020-10-23
Among them, P10D represents the prefix P (representing the time interval), and 10 represents the number of days in the interval.
In addition, you can also use the modify() method to add and subtract dates. For example, get the time after 3 hours:
$dateTime->modify('+3 hours'); echo $dateTime->format('Y-m-d H:i:s');
Output:
2020-10-13 12:43:39
4. Time zone processing
In PHP, timestamps and date and time strings are related to time zones . By default, PHP uses the server's time zone setting. However, during development, you may need to deal with times in different time zones, in which case the time zone needs to be processed.
In PHP, you can use the date_default_timezone_set() function to set the time zone, for example:
date_default_timezone_set('Asia/Shanghai');
After setting, all places that use the date() function to obtain date and time will use the specified time zone. Similarly, for timestamp processing, the DateTime class and related methods should also be used to avoid time zone issues.
In addition, if you need to convert between different time zones, you can also use the setTimeZone() method and getTimeZone() method provided by the DateTime class. For example, to convert a date and time object in the current time zone to a date and time object in another time zone:
$dateTime = new DateTime('2020-10-13 09:43:39', new DateTimeZone('Asia/Shanghai')); echo $dateTime->format('Y-m-d H:i:s'); // 输出:2020-10-13 09:43:39 $dateTime->setTimeZone(new DateTimeZone('America/New_York')); echo $dateTime->format('Y-m-d H:i:s'); // 输出:2020-10-12 21:43:39
Set the target time zone through the setTimeZone() method, and then format it into a string through the format() method.
5. Summary
This article introduces date and time operations in PHP, including timestamps, date formatting, date calculations, and time zone processing. In actual development, mastering these operations is very useful and can improve development efficiency and program robustness. I hope this article will be helpful to PHP developers.
The above is the detailed content of Guide to Date and Time Manipulation in PHP. For more information, please follow other related articles on the PHP Chinese website!