Difference Between Two Dates - Php

How to find difference between two dates in php?

Snippet Code

Rate this page :
  [ 0 votes]

Find the number of days, hours, months and seconds between two dates given through the function date_difference() using diff() method.

<?php function date_difference($date1,$date2) { $date2->format('Y-m-d H:i:s'); $left = $date1->diff($date2); $d= $left->format('%a'); $h= $left->format('%h'); $m= $left->format('%i'); $s= $left->format('%s'); return $d."days : ".$h."hours :".$m."mins :".$s."secs"; } $d1 = new DateTime('2015-06-30 12:48:50'); $d2 = new DateTime('2015-12-20 22:10:10'); echo date_difference($d1,$d2); ?>


Ask Questions

Ask Question