Hướng dẫn php dateinterval add days
PHP has a lot of methods and functions to help you manipulate dates in a variety of ways. In this quick tip, I will show you how to add days to a date in PHP using the Show The DateInterval ClassThe The following table lists all the characters used to designate specific periods.
You might have noticed that M represents both months and minutes, but the ambiguity is resolved by the character T that I mentioned earlier. Since minutes represent the time component of our interval, they will be preceded by a T. Here are some examples to make things clear.
There are two things that you have to remember when specifying the duration.
Adding Days to a Date With PHP DateIntervalNow that we know how to specify date intervals, we will learn how to add days to a date in PHP. We can use the format('M d, Y H:i:s'); // Dec 15, 2021 00:00:00 $interval = new DateInterval('P18DT6M'); $date->add($interval); echo $date->format('M d, Y H:i:s'); // Jan 02, 2022 00:06:00 ?> We start with December 15, 2021 as our date stored as a The format('M d, Y'); // Jan 29, 2022 $interval = new DateInterval('P25D'); for($i = 0; $i <= 4; $i++) { $date->add($interval); echo "\n".$date->format('M d, Y'); } /* Feb 23, 2022 Mar 20, 2022 Apr 14, 2022 May 09, 2022 Jun 03, 2022 */ ?> You can see from the following example that the output changes when the initial date is from a leap year. format('M d, Y'); // Jan 29, 2024 $interval = new DateInterval('P25D'); for($i = 0; $i <= 4; $i++) { $date->add($interval); echo "\n".$date->format('M d, Y'); } /* Feb 23, 2024 Mar 19, 2024 Apr 13, 2024 May 08, 2024 Jun 02, 2024 */ ?> Specifying Weeks and Days Together in a DateIntervalCombining W with D when creating a date interval has only been possible since PHP 8.0.0. This means that different versions of PHP will return different dates when running the same code. Here is an example: format('M d, Y H:i:s')."\n"; $interval = new DateInterval('P2W2DT25H'); for($i = 0; $i <= 4; $i++) { $date->add($interval); echo "\n".$date->format('M d, Y H:i:s'); } ?> Two things to note here are the use of W and D together, and that the number of hours in the interval is above 24. This effectively makes the interval (14 + 2 + 1 = ) 17 days and 1 hour. So PHP should add that with each iteration. The output when running the above code in PHP 8.0.3 is as expected. Jan 29, 2024 00:00:00 Feb 15, 2024 01:00:00 Mar 03, 2024 02:00:00 Mar 20, 2024 03:00:00 Apr 06, 2024 04:00:00 Apr 23, 2024 05:00:00 However, PHP 7.4 only adds 3 days and 1 hour with each iteration while ignoring the 2 weeks period. Jan 29, 2024 00:00:00 Feb 01, 2024 01:00:00 Feb 04, 2024 02:00:00 Feb 07, 2024 03:00:00 Feb 10, 2024 04:00:00 Feb 13, 2024 05:00:00 Final ThoughtsI have only mentioned the Did you find this post useful? Freelancer, Instructor I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around. |