/** * Accessor for vacationsLeft property * * @return int */ public function getVacationsLeftAttribute() { $now = new Carbon(); $hired = new Carbon($this->hired); if ($hired < $now) { $weeksWorked = $now->diffInWeeks($hired); $totalVacations = 28 / 52 * $weeksWorked; $vacationsTaken = $this->vacations()->sum('duration'); return (int) $totalVacations - $vacationsTaken; } return 0; }
/** * Get the difference between start and end dates of the period in weeks. * * @return int */ public function lengthInWeeks() { return $this->startDate->diffInWeeks($this->endDate); }
<?php require_once 'vendor/autoload.php'; use Carbon\Carbon; use Citco\Carbon as CitcoCarbon; use CarbonExt\FiscalYear\Calculator; // Object Instantiation $brisbane = new Carbon('2015-12-01', 'Australia/Brisbane'); $newYorkCity = new Carbon('2015-12-01', 'America/New_York'); $dtBerlin = new Carbon('2015-12-01', 'Europe/Berlin'); $outputString = "Time difference between %s & %s: %s hours.\n"; // Date difference printf($outputString, "Berlin", "Brisbane, Australia", $dtBerlin->diffInHours($brisbane, false)); printf($outputString, "Berlin", "New York City, America", $dtBerlin->diffInHours($newYorkCity, false)); $septEighteen2014 = Carbon::createFromDate(2014, 9, 18, $dtBerlin->getTimezone()); printf("difference between now and %s in \n\thours: %d, \n\tdays: %d, \n\tweeks: %d, \n\tweekend days: %d, \n\tweek days: %s, \n\thuman readable: %s\n", $septEighteen2014->toFormattedDateString(), $dtBerlin->diffInHours($septEighteen2014), $dtBerlin->diffInDays($septEighteen2014), $dtBerlin->diffInWeeks($septEighteen2014), $dtBerlin->diffInWeekendDays($septEighteen2014), $dtBerlin->diffInWeekDays($septEighteen2014), $dtBerlin->diffForHumans($septEighteen2014)); // Date formatting echo $dtBerlin->toDateString() . "\n"; echo $dtBerlin->toFormattedDateString() . "\n"; echo $dtBerlin->toTimeString() . "\n"; echo $dtBerlin->toDateTimeString() . "\n"; echo $dtBerlin->toDayDateTimeString() . "\n"; echo $dtBerlin->toRfc1036String() . "\n"; echo $dtBerlin->toAtomString() . "\n"; echo $dtBerlin->toCookieString() . "\n"; echo $dtBerlin->toRssString() . "\n"; $dtBerlin->setToStringFormat('l jS \\of F Y'); echo $dtBerlin . "\n"; echo (int) $dtBerlin->isLeapYear() . "\n"; // is* range of functions test printf("Is yesterday? %s\n", $dtBerlin->isYesterday() ? "yes" : "no");
public function getProfile($userid) { $user = User::find($userid); if (count($user) >= 1) { $updated = new Carbon($user->updated_at); $now = Carbon::now(); if ($updated->diff($now)->m < 1) { $lastOnline = "Last seen less than a minute ago"; } elseif ($updated->diff($now)->h < 1) { $lastOnline = $updated->diffInMinutes($now) > 1 ? sprintf("Last seen %d minutes ago", $updated->diffInMinutes($now)) : sprintf("Last seen %d minute ago", $updated->diffInMinutes($now)); } elseif ($updated->diff($now)->d < 1) { $lastOnline = $updated->diffInHours($now) > 1 ? sprintf("Last seen %d hours ago", $updated->diffInHours($now)) : sprintf("Last seen %d hour ago", $updated->diffInHours($now)); } elseif ($updated->diff($now)->d < 7) { $lastOnline = $updated->diffInDays($now) > 1 ? sprintf("Last seen %d days ago", $updated->diffInDays($now)) : sprintf("Last seen %d day ago", $updated->diffInDays($now)); } elseif ($updated->diff($now)->m < 1) { $lastOnline = $updated->diffInWeeks($now) > 1 ? sprintf("Last seen %d weeks ago", $updated->diffInWeeks($now)) : sprintf("Last seen %d week ago", $updated->diffInWeeks($now)); } elseif ($updated->diff($now)->y < 1) { $lastOnline = $updated->diffInMonths($now) > 1 ? sprintf("Last seen %d months ago", $updated->diffInMonths($now)) : sprintf("Last seen %d month ago", $updated->diffInMonths($now)); } else { $lastOnline = $updated->diffInYears($now) > 1 ? sprintf("Last seen %d years ago", $updated->diffInYears($now)) : sprintf("Last seen %d year ago", $updated->diffInYears($now)); } return view('dashboard.userProfile', ['user' => $user, 'lastOnline' => $lastOnline]); } else { return view('dashboard.userProfile'); } }