I have a laravel app in which i'm getting data from python software
data i'm getting from software - clock in time(when user sign in on software) & clock out time(when user is living the software)
what i want to achieve - i want to get the amount of time user spend in software for example:- clock in time = 5:00 PM
& clock out time = 6:10 PM
so the user has spend 1 hour & 10 minutes
on the software so how can we write this login in Laravel or PHP
(note - time is in hour-minute formet)
CodePudding user response:
You can use Carbon built-in package advantages for this. This is an amazing package for working with times/dates. As their official documentation says: you can do something like this:
// Don't forget to include these at the top
use Carbon\Carbon;
use Carbon\CarbonInterface;
// ***
$in = explode(' ', "clock in time = 5:00 PM");
$out = explode(' ', "clock out time = 6:10 PM");
$start_time = $in[4] . ' ' . $in[5];
$end_time = $out[4] . ' ' . $out[5];
// if you can get exact "5:00 AM" and "6:10 AM" from software,
// then you can start from here immediately
$start = Carbon::parse($start_time); // "5:00 AM"
$end = Carbon::parse($end_time); // "6:10 AM"
// this will return "1 hour, 10 minutes"
return $end->diffForHumans($start, [
'parts' => 2,
'join' => ', ',
'syntax' => CarbonInterface::DIFF_ABSOLUTE, // this will show with "after"/"before" syntax
]);