PHP date differences in months, days and year using PHP Datetime()ĭateTime class was introduced in PHP 5.2. The formula is ( second in a minute*minutes in an hour). I use the same formula as above except for hours. Output : hours difference between -07 = 6408Īs you can see, I convert two date differences in hours using PHP. Printf("hours difference between %s and %s = %d", $fromDate, $curDate, $hour) The following code help to get the Hours difference between two dates. PHP Difference between two dates in Hours I am using the function strtotime() that will convert date into unix time-stamp that’s why I am getting the difference between two dates in a timestamp.įinally, I am converting this timestamp into days using the formula ( second in a minute*minutes in an hour*hour in a day). I subtract the current date from the previous date and use abs() php method for absolute value. Output : Days difference between -30 = 260 Printf("Days difference between %s and %s = %d", $fromDate, $curDate, $days) $daysLeft = abs(strtotime($curDate) - strtotime($fromDate))
0 Comments
Leave a Reply. |