The following Code will differenciate between date post and current time
<?php
date_default_timezone_set('Africa/Lagos');
function convert($datetime){
$time=strtotime($datetime);
$diff=time()-$time;
$diff/=60;
$var1=floor($diff);
$var=$var1<=1 ? 'min' : 'mins';
if($diff>=60){
$diff/=60;
$var1=floor($diff);
$var=$var1<=1 ? 'hr' : 'hrs';
if($diff>=24){$diff/=24;$var1=floor($diff);$var=$var1<=1 ? 'day' : 'days';
if($diff>=30.4375){$diff/=30.4375;$var1=floor($diff);$var=$var1<=1 ? 'month' : 'months';
if($diff>=12){$diff/=12;$var1=floor($diff);$var=$var1<=1 ? 'year' : 'years';}}}}
echo $var1,' ',$var,' ago';
}
$date = date('Y-m-d H:i:s');
echo $date;
echo "<br>";
$datetime="2020-04-27 12:00:00";
echo convert($datetime);
?>
Leave a Reply
Your email address will not be published. Required fields are marked *