Saved X
Google login
SignUp with Email
X

Time Difference like Instagram & youtube php

theindreshverma ( Edited  26 Oct 2021, 12:26 am )
Login

$date = '18-11-2017 05:00:49 PM';   
$current_time = '18-01-2021 06:40:53 PM';

$time1 = new DateTime($date);
$time2 = new DateTime($current_time);
$timediff = $time1->diff($time2);

echo $timediff->format('%y year %m month %d days %h hour %i minute %s second')."<br/>";
 
$second = $timediff->s;
$minute = $timediff->i;
$hour = $timediff->h;
$days = $timediff->d;
$month = $timediff->m;
$year  = $timediff->y;
 
if ($year>0) {
   echo $year.' Year ago.';
}
elseif ($month>0) {
   echo $month.' Month ago.';
}
elseif ($days>0) {
   echo $days.' Days ago.';
}
elseif ($hour>0) {
   echo $hour.' Hours ago.';
}
elseif ($minute>0) {
   echo $minute.' Minute ago.';
}
elseif ($second>0) {
   echo $second.' Second ago.';
}
else{

}

result

3 year 2 month 0 days 1 hour 40 minute 4 second
3 Year ago.

Sourcecode Tags

More Sourcecodes

More Sourcecodes