How To Get Difference Between Two Dates In PHP?
In this code model we are taking two dates and getting their disparities, following code can be utilized to get definite date distinction in days between two dates.
We may at some point require to get a contrast between in PHP for your application, use any PHP framework like Laravel, Codeigniter, Wordpress but you can use code PHP code anywhere. So you can see the following examples.
Bellow examples give you to calculate the difference between two dates in PHP example.
Example 1
<?php
$startDate = "2018-01-28";
$endDate = "2020-08-19";
$second1year = 365*60*60*24;
$second1month = 30*60*60*24;
$second1day = 60*60*24;
$diffData = abs(strtotime($endDate) - strtotime($startDate));
$yearsDiff = floor($diffData / $second1year);
echo ("Years:".$yearsDiff);
$monthsDiff = floor(($diffData - $yearsDiff * $second1year) / ($second1month));
echo (" Months:".$monthsDiff);
$daysDiff = floor(($diffData - $yearsDiff * $second1year - $monthsDiff* $second1month)/ ($second1day));
echo (" Days:".$daysDiff);
?>
- <?php
-
- $startDate = "2018-01-28";
- $endDate = "2020-08-19";
- $second1year = 365*60*60*24;
- $second1month = 30*60*60*24;
- $second1day = 60*60*24;
- $diffData = abs(strtotime($endDate) - strtotime($startDate));
- $yearsDiff = floor($diffData / $second1year);
- echo ("Years:".$yearsDiff);
-
- $monthsDiff = floor(($diffData - $yearsDiff * $second1year) / ($second1month));
- echo (" Months:".$monthsDiff);
-
- $daysDiff = floor(($diffData - $yearsDiff * $second1year - $monthsDiff* $second1month)/ ($second1day));
- echo (" Days:".$daysDiff);
- ?>
<?php
$startDate = "2018-01-28";
$endDate = "2020-08-19";
$second1year = 365*60*60*24;
$second1month = 30*60*60*24;
$second1day = 60*60*24;
$diffData = abs(strtotime($endDate) - strtotime($startDate));
$yearsDiff = floor($diffData / $second1year);
echo ("Years:".$yearsDiff);
$monthsDiff = floor(($diffData - $yearsDiff * $second1year) / ($second1month));
echo (" Months:".$monthsDiff);
$daysDiff = floor(($diffData - $yearsDiff * $second1year - $monthsDiff* $second1month)/ ($second1day));
echo (" Days:".$daysDiff);
?>
Example 2
<?php
$date1="2018-01-28";
$date2="2020-08-19";
function dateDiff($date1, $date2)
{
$date1_new = strtotime($date1);
$date2_new = strtotime($date2);
$diff = $date2_new - $date1_new;
return round($diff / 86400); //60*60*24 (1day second)
}
$dateDiff= dateDiff($date1, $date2);
printf("Difference between in two dates : " .$dateDiff. " Days ");
print "</br>";
?>
- <?php
- $date1="2018-01-28";
- $date2="2020-08-19";
- function dateDiff($date1, $date2)
- {
- $date1_new = strtotime($date1);
- $date2_new = strtotime($date2);
- $diff = $date2_new - $date1_new;
- return round($diff / 86400); //60*60*24 (1day second)
- }
- $dateDiff= dateDiff($date1, $date2);
- printf("Difference between in two dates : " .$dateDiff. " Days ");
- print "</br>";
- ?>
<?php
$date1="2018-01-28";
$date2="2020-08-19";
function dateDiff($date1, $date2)
{
$date1_new = strtotime($date1);
$date2_new = strtotime($date2);
$diff = $date2_new - $date1_new;
return round($diff / 86400); //60*60*24 (1day second)
}
$dateDiff= dateDiff($date1, $date2);
printf("Difference between in two dates : " .$dateDiff. " Days ");
print "</br>";
?>
I hope it can help you...
Robert look
Author, Web Developer (Front-end & Back-end)
My Name is Robert look from New york, America. I'm a Web Application Developer with 6 years of experience. Currently, I'm working on front-end & back-end web development using popular web technology Languages. That's why I love to share my coding knowledge in a smart way.
Leave a Reply