Created
February 27, 2016 22:16
-
-
Save bramburn/05857e84d4827ceef4bb to your computer and use it in GitHub Desktop.
Laravel + Carbon difference between two dateTime column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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