How to calculate the difference between two dates using PHP?

Today, We want to share with you difference between two dates in php.In this post we will show you php date difference in months, hear for Calculate the difference between two dates (in years, months, days) we will give you demo and example for implement.In this post, we will learn about How to find number of days between two dates using php? with an example.

PHP date_diff() Function

Example 1: use strtotime() to convert two dates to unix time

$start_date = "2007-03-24";
$end_date = "2009-06-26";

$diff = abs(strtotime($end_date) - strtotime($start_date));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Example 2: use DateTime and DateInterval objects.

$start_date = new DateTime("2007-03-24");
$end_date = new DateTime("2009-06-26");
$time_periods = $start_date->diff($end_date);
echo "difference " . $time_periods->y . " years, " . $time_periods->m." months, ".$time_periods->d." days "; 

// shows the total amount of days (not divided into years, months and days like above)
echo "difference " . $time_periods->days . " days ";

Get the Number of Days Between Two Dates

$start_date = new DateTime('1988-11-09');
$end_date = new DateTime('2023-05-09');
$time_periods = $start_date->diff($end_date);

// Results — 34 years 06 months and 00 days.
echo $time_periods->format('%Y years %M months and %D days.');

// Results — Total number of days passed: 12599.
echo $time_periods->format('Total number of days passed: %a.');

Calculate the Number of Hours Between Two Dates

$start_date = new DateTime('2021-01-09T11:56:59+00:00');
$end_date = new DateTime('2021-01-09T17:19:29+00:00');
$time_periods = $start_date->diff($end_date);

// Results — (+) 05 hours 22 minutes and 30 seconds.
echo $time_periods->format('(%R) %H hours %I minutes and %S seconds.');

$start_date = new DateTime('2021-01-09T11:56:59+00:00');
$end_date = new DateTime('2021-01-09T17:19:29-08:00');
$time_periods = $start_date->diff($end_date);

// Results — (+) 13 hours 22 minutes and 30 seconds.
echo $time_periods->format('(%R) %H hours %I minutes and %S seconds.');

$start_date = new DateTime('2021-01-09T11:56:59+00:00');
$end_date = new DateTime('2021-01-09T17:19:29+08:00');
$time_periods = $start_date->diff($end_date);

// Results — (-) 02 hours 37 minutes and 30 seconds.
echo $time_periods->format('(%R) %H hours %I minutes and %S seconds.');
$start_date = new DateTime('2021-01-19T11:56:59+00:00');
$end_date = new DateTime('2021-01-09T17:19:29+08:00');
$time_periods = $start_date->diff($end_date);

$total_added_days = $time_periods->format('%a');
$hours_diff = $time_periods->format('%H');

// Results — Total Days Passed: 10
echo 'Total Days Passed: '.$total_added_days;

// Results — Hour Difference in Two Times: 02
echo 'Hour Difference in Two Times: '.$hours_diff;

// Results — Total Hours Passed: 242
echo 'Total Hours Passed: '.($total_added_days*24 + $hours_diff);
$start_date = new DateTime('2021-01-09T11:56:59+00:00');
$end_date = new DateTime('2021-01-19T17:19:29+08:00');
$time_periods = $start_date->diff($end_date);

$total_added_days = $time_periods->format('%a');
$hours_passed = $time_periods->format('%H');

// Results — Total Days Passed: 9
echo 'Total Days Passed: '.$total_added_days;

// Results — Hour Difference in Two Times: 21
echo 'Hour Difference in Two Times: '.$hours_passed;

// Results — Total Hours Passed: 237
echo 'Total Hours Passed: '.($total_added_days*24 + $hours_passed);

Calculate the Number of Minutes Between Two Dates

$start_date = new DateTime('2021-01-19T11:56:59+00:00');
$end_date = new DateTime('1993-10-09T17:34:22+08:00');
$time_periods = $start_date->diff($end_date);

$total_added_days = $time_periods->format('%a');
$hours_diff = $time_periods->format('%H');
$minutes_diff = $time_periods->format('%I');
$total_minutes = (($total_added_days*24 + $hours_diff) * 60 + $minutes_diff);

// Results — Total Days Passed: 8868
echo 'Total Days Passed: '.$total_added_days;

// Results — Hour Difference in Two Times: 02
echo 'Hour Difference in Two Times: '.$hours_diff;

// Results — Minute Difference in Two Times: 22
echo 'Minute Difference in Two Times: '.$minutes_diff;

// Results — Total Minutes Passed: 12770062
echo 'Total Minutes Passed: '.$total_minutes;

Calculate the Number of Seconds Between Two Dates

$start_date = new DateTime('2021-01-19T00:00:00+00:00');
$end_date = new DateTime('1994-02-25T17:34:22+05:30');
$time_periods = $start_date->diff($end_date);

$total_added_days = $time_periods->format('%a');
$hours_diff = $time_periods->format('%H');
$minutes_diff = $time_periods->format('%I');
$seconds_diff = $time_periods->format('%S');
$total_minutes = (($total_added_days*24 + $hours_diff) * 60 + $minutes_diff);
$total_seconds = $total_minutes*60 + $seconds_diff;

// Results — Total Days Passed: 8728
echo 'Total Days Passed: '.$total_added_days;

// Results — Hour Difference in Two Times: 11
echo 'Hour Difference in Two Times: '.$hours_diff;

// Results — Minute Difference in Two Times: 55
echo 'Minute Difference in Two Times: '.$minutes_diff;

// Results — Seconds Difference in Two Times: 38
echo 'Seconds Difference in Two Times: '.$seconds_diff;

// Results — Total Seconds Passed: 754142138
echo 'Total Seconds Passed: '.$total_seconds;

I hope you get an idea about current date in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment