Demo Online
- 5 years ago
- Zaid Bin Khalid
- 4892 Views
-
4
In this tutorial, I am going to show you how you can take two dates difference in Years, Months, Weeks, Days, Hours, Minutes and in Seconds. For that, I create a function that returns all of these. You just need to pass 3 parameters. First one is what you want and others are the start date and end date respectively.
Below code is a function that you can use to get dates difference.
// Date Difference Funciton
function dateDiff($getIn,$startDate,$endDate) {
$dateTimeBegin = strtotime($dateTimeBegin);
if($dateTimeBegin === -1) {
return("..begin date Invalid");
}
$dateTimeEnd = strtotime($dateTimeEnd);
if($dateTimeEnd === -1) {
return("..end date Invalid");
}
$dif = $dateTimeEnd - $dateTimeBegin;
switch($interval){
case "s"://seconds
return($dif);
case "n"://minutes
return(floor($dif/60)); //60s=1m
case "h"://hours
return(floor($dif/3600)); //3600s=1h
case "d"://days
return(floor($dif/86400)); //86400s=1d
case "w"://Week
return(floor($dif/604800)); //604800s=1week=1semana
case "m": //similar result "m" dateDiff Microsoft
$monthBegin = (date("Y",$dateTimeBegin)*12)+date("n",$dateTimeBegin);
$monthEnd = (date("Y",$dateTimeEnd)*12)+date("n",$dateTimeEnd);
$monthDiff = $monthEnd-$monthBegin;
return($monthDiff);
case "y": //similar result "yyyy" dateDiff Microsoft
return(date("Y",$dateTimeEnd) - date("Y",$dateTimeBegin));
default:
return(floor($dif/86400)); //86400s=1d
}
}
Copy and paste the above code into your functions or main file. And after that, you are able to use this function like below code show example of usage. In below code, I pass d which represents days so the function will return days.
In above function s represent seconds, n for minutes, h for hours, d for days, w weeks, m months and y for years.
echo dateDiff('d','2019-01-01','2019-02-02');
32 //OUTPUT IN DAYS
PHP date_diff() Function
You can also do that job with using date_diff() function. This is a PHP default function and this function will return an array with all necessary objects.
$startDate = '2019-01-01';
$endDate = '2019-04-01';
$date1 = date_create($startDate);
$date2 = date_create($endDate);
$diff = date_diff($date1,$date2);
print"<pre>";
print_r($diff);
print"</pre>";
//The output is given below.
DateInterval Object
(
[y] => 0
[m] => 3
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 90
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
echo $diff->y; //Get year
echo $diff->m; //Get month
echo $diff->d; //Get days
echo $diff->days; //Get total days
PHP DateTime() Class
Here is the third example which is Object-oriented. You can also do the same job using the PHP DateTime() Class. The below code is the working example.
$datetime1 = new DateTime('2019-01-11');
$datetime2 = new DateTime('2019-01-13');
$interval = $datetime1->diff($datetime2);
print"<pre>";
print_r($interval);
print"</pre>";
//The output is given below.
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 2
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 2
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
Demo Online
- 5 years ago
- Zaid Bin Khalid
- 4892 Views
-
4