How to get number of days between two dates in PHP

How to get number of days between two dates in php?

0 2031

You can find out the number of days between two dates from your database.

$today= date('d-m-Y'); // get today's date
// here you can also change today's date according to your requirement

$timestamp= date('Y-m-d',strtotime($fetch['timestamp']));
// here timestamp value has been retrieved from database

$datetime1 = new DateTime("$timestamp");
$datetime2 = new DateTime("$today");
$difference = $datetime1->diff($datetime2); // compare both dates

$days= 'Difference: '.$difference->days.' days';
// get the days difference in a variable

echo $days; // Print the days where you want to show

// You can also get the difference in year, months
// to get the year difference, use below code

$years= 'Difference: '.$difference->y.' years';

echo $years;

// to get the month difference, use below code

$months= 'Difference: '.$difference->m.' months';

echo $months;

For any technical query and support, you can share your question with us through below comment section. Our technical team always happy to serve you !!


Leave a Reply

    Waiting for your comments