In this article, you will learn how to create a DateInterval from a string. The PHP date_interval_create_from_date_string() function takes relative parts from the string to create the DateInterval.
What is the syntax of the date_interval_create_from_date_string() function in PHP?
date_interval_create_from_date_string(datetime)
Parameter | Details |
---|---|
datetime | DateInterval – Required |
Examples of the date_interval_create_from_date_string() function
Example 1. In the following example, we create a date and then create an Interval using the date_interval_create_from_date_string function.
<?php
$d= date_create('2021-01-04');
date_add($date, date_interval_create_from_date_string('2 years 15 days'));
echo date_format($d, 'Y-m-d');
?>