At first we need to create two
datetime
objects using
createFromFormat
static method of
DateTime
built-in class
$first_date = DateTime::createFromFormat('Y-m-d', "2020-12-23");
$second_date = DateTime::createFromFormat('Y-m-d', "2020-12-30");
Using
diff()
method of
DateTime
class we actually calculate differences between the two dates and we get the result in days
$difference_between_the_days = $second_date->diff($first_date)->format("%a");
	
echo $difference_between_the_days;
by Valeri Tandilashvili
4 years ago
PHP
datetime
2
Pro tip: use ```triple backticks around text``` to write in code fences