Skip to content

Instantly share code, notes, and snippets.

@bramburn
Created February 27, 2016 22:16
Show Gist options
  • Save bramburn/05857e84d4827ceef4bb to your computer and use it in GitHub Desktop.
Save bramburn/05857e84d4827ceef4bb to your computer and use it in GitHub Desktop.
Laravel + Carbon difference between two dateTime column
public function DurationOfTimesheet($timesheet_id, $output = null) {
$time = $this->where('id', $timesheet_id)->first();
if (empty($time)) {
return "timesheet not found";
} else {
// dd($time->start);
$start = Carbon::createFromFormat('Y-m-d H:i:s', $time->start);
if (!(int) ($time->finish)) {
$finish = Carbon::now();
// we give them the current time
} else {
//or else if we have logged out we show the difference between the two columns
$finish = Carbon::createFromFormat('Y-m-d H:i:s', $time->finish);
}
$difference = $start->diff($finish);
if ($output == true) {
return $difference;
} else {
$humanoutput = "";
if ($difference->d) {
$humanoutput .= $difference->d . " day(s), ";
}
if ($difference->h) {
$humanoutput .= $difference->h . " hour(s), ";
}
if ($difference->i) {
$humanoutput .= $difference->i . " min(s), ";
}
return rtrim($humanoutput, ', ');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment