PHP8.1’s new static methods of the DateTime class
The PHP8.1 version introduces some powerful features and functions. One of the eye-catching updates is the static method of the DateTime class. The DateTime class is an important tool for processing dates and times in PHP. It provides many practical methods to operate and process date and time data. Let's take a look at some of the new static methods of the DateTime class in PHP8.1 and their usage examples.
Code example:
$immutable = new DateTimeImmutable('2022-01-01'); $mutable = DateTime::createFromImmutable($immutable); echo $mutable->format('Y-m-d'); // Output: 2022-01-01 $mutable->modify('+1 day'); echo $mutable->format('Y-m-d'); // Output: 2022-01-02
Code example:
$start = new DateTime('2022-01-01'); $end = new DateTime('2022-01-10'); $duration = $start->diff($end); echo DateTime::formatDuration($duration); // Output: 9 days
Code example:
$date = DateTime::createFromFormat('Y/m/d', '2022/20/01'); $errors = DateTime::getLastErrors(); print_r($errors);
The above are some new static methods of the DateTime class added in PHP8.1. These methods provide more flexibility and convenience for working with datetime data. By using these methods, we can manipulate and process date-time data more efficiently. If you are using PHP8.1 or plan to upgrade to this version, these new features will definitely bring you a lot of benefits.
The above is the detailed content of New static method of DateTime class in PHP8.1. For more information, please follow other related articles on the PHP Chinese website!