here i want to do employee login time calculation,like
- My office login time is
2018-02-01 09:30 AM
- But my logged in time
2018-02-01 09:40 AM
based on above two dates i came to office 10 Mins late
.
suppose my logged in time 2018-02-01 10:40 AM
(1 Hr and 20 mins late)
My expected results (Mins late means)
10 Mins late
My expected results (Hrs late means)
1 Hrs late
I tried like this but it is not working properly
date_default_timezone_set('UTC');
$officeTiming ="2018-02-01 09:30 AM";
$todaymyloginTime ="2018-02-01 09:40 AM";
$datetime1 = date_create($officeTiming);
$datetime2 = date_create($todaymyloginTime);
$interval = date_diff($datetime1, $datetime2);
$suffix = ' Late';
if ($datetime1 > $datetime2) $suffix = ' Before';
$time_diff= $interval->format('%h:%i:%s');
$start_date = new DateTime($time_diff);
$since_start = $start_date->diff(new DateTime(date("h:i:s")));
if(intval($since_start->format('%h')) >= 1){
$timeago = $since_start->format('%h hours'.$suffix);
}
else if(intval($since_start->format('%i')) >= 1){
$timeago = $since_start->format('%i minutes'.$suffix);
}
else if(intval($since_start->format('%s')) >= 1){
$timeago = $since_start->format('%s seconds'.$suffix);
}
echo $timeago;
I am getting answer is 4 hours Late but actually i am 20 mins late
My working code
$datetime1 = new DateTime('2018-02-01 09:30 AM');
$datetime2 = new DateTime('2018-02-01 09:20 AM');
$interval = $datetime1->diff($datetime2);
$suffix = ' Late';
if ($datetime1 > $datetime2) $suffix = ' Before';
if($interval->format('%h') ==0){
echo $interval->format('%i')." Minutes".$suffix;
}else{
echo $interval->format('%h')." Hours".$suffix;
}