PHP DateTime Difference (Hours, Days, Weeks, Months) -
i creating php function return difference between 2 dates in format: 2 months, 3 weeks, 6 days, 3 hours. have tried use php datetime class, returns months, days , hours , can not find way calculate weeks.
this function:
public function datetimedifference($fromdate, $todate) { $fromdate = new datetime($fromdate); $todate = new datetime($todate); $interval = $fromdate->diff($todate); $difference["hours"] = $interval->h; $difference["days"] = $interval->d; $difference["months"] = $interval->m; return $difference; }
now, need $difference["weeks"] included in return data.
edit: know can divide days 7 , weeks, not result right. example: 2 months, 14 days, 3 hours - when divide 14 days 7 this: 2 months, 2 weeks, 14 days, 3 hours , not same period.
public function datetimedifference($fromdate, $todate) { $fromdate = new datetime($fromdate); $todate = new datetime($todate); $interval = $fromdate->diff($todate); $difference["hours"] = $interval->h; $difference["weeks"] = floor($interval->d/7); $difference["days"] = $interval->d % 7; $difference["months"] = $interval->m; return $difference; }
Comments
Post a Comment