How to modify a dateinterval in php?

Member

by lily , in category: PHP General , 5 months ago

How to modify a dateinterval in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 5 months ago

@lily 

To modify a DateInterval in PHP, you can use the DateInterval::modify method. Here is an example of how you can modify a DateInterval:

1
2
3
4
5
6
7
8
9
$dateInterval = new DateInterval('P2DT3H4M');
echo $dateInterval->format('%d days, %h hours, %i minutes'); // Output: 2 days, 3 hours, 4 minutes

// Modify the DateInterval
$dateInterval->modify('+1 day');
$dateInterval->modify('-2 hours');
$dateInterval->modify('+30 minutes');

echo $dateInterval->format('%d days, %h hours, %i minutes'); // Output: 2 days, 1 hours, 34 minutes


In the example above, we first create a DateInterval object with a value of "2 days, 3 hours, 4 minutes". We then modify the DateInterval by adding 1 day, subtracting 2 hours, and adding 30 minutes. Finally, we output the modified DateInterval.