PHP Difference Between Two Dates

Today, I will tell you about difference of days in two given dates using php.There are many methods to get days in PHP, you can use DATETIME() function to get days difference between two dates in php,but in this tutorial i will use strtotime php function to convert date into timestamp then subtract startDate from endDate.

You can also check other recommended Date PHP tutorials,

Days Difference Between two dates in PHP

output : Days difference between 2016-03-15 and 2016-11-30 = 260

I have subtract current date to previous data and use abs() php method for absolute value.I am using strtotime() function that will convert date into unix time-stamp that’s why i am getting difference between two dates in a timestamp, finally i am converting this timestamp into days using formula (second in a minute*minutes in an hour*hour in a day).

PHP Difference between two dates in Hours

output : hours difference between 2016-03-15 and 2016-12-07 = 6408

as you can see, I convert two date differences in hours using php.I use same formula as above except hours.The formula is (second in a minute*minutes in an hour).

PHP date differences in months,days and year using PHP Datetime()

DateTime class was introduced in PHP 5.2.Its better than old date() and time() functions.DateTime() takes two parameters the first is the time value, you can use a date format, unix timestamp, a day interval or a day period. The second parameter is the timezone that you want to assign the date value.

PHP Difference in Months Between two Dates

output : 8 months