php
PHP program to find the number of days between two dates
Trying to get the number of days between two dates? You can use the methods explained in this post.
<?php
// If you are using PHP 5.3 or above
$date_1 = new DateTime("2021-09-20");
$date_2 = new DateTime("2021-10-22");
$num_of_days = $date_1->diff($date_2)->format("%a");
echo $num_of_days;
?>
Output
32
Get the number of days between dates using strtotime()
You can also use the strtotime() function to calculate the number of days between two dates. You just need to calculate the difference between the given dates and divide them to (60 * 60 * 24).
Code Example
<?php
$date_a = strtotime("2021-03-27");
$date_b = strtotime("2021-04-25");
$diff_in_dates = $date_b - $date_a;
echo round($diff_in_dates / (60 * 60 * 24));
?>
Output
29
Was this helpful?
Similar Posts
- PHP program to generate a token that is cryptographically secured
- PHP program to get the values of checkboxes on Form Submit
- Join or concatenate two or multiple strings in PHP
- Validate a number using filter_var() method in PHP
- Convert characters to lowercase using strtolower() function in PHP
- Check empty string in PHP
- Calculate length of a string using strlen() function in php