jcadima
4/1/2018 - 7:42 PM

Carbon examples

https://scotch.io/tutorials/easier-datetime-in-laravel-and-php-with-carbon

// get the current time
$current = Carbon::now();

// add 30 days to the current time
$trialExpires = $current->addDays(30);


// Common date time string used for our projects
{{ \Carbon\Carbon::parse($history->created_at)->toDayDateTimeString() }}
// prints: Fri, May 11, 2018 3:00 PM

// FORMATTING
$dt = Carbon::now();

echo $dt->toDateString();               // 2015-12-19
echo $dt->toFormattedDateString();      // Dec 19, 2015
echo $dt->toTimeString();               // 10:10:16
echo $dt->toDateTimeString();           // 2015-12-19 10:10:16
echo $dt->toDayDateTimeString();        // Sat, Dec 19, 2015 10:10 AM

// ... of course format() is still available
echo $dt->format('l jS \\of F Y h:i:s A');         // Saturday 19th of December 2015 10:10:16 AM



// DIFFERENCE FOR HUMANS
$dt     = Carbon::now();
$past   = $dt->subMonth();
$future = $dt->addMonth();

echo $dt->subDays(10)->diffForHumans();     // 10 days ago
echo $dt->diffForHumans($past);             // 1 month ago
echo $dt->diffForHumans($future);           // 1 month before


// CURRENT TIMEZONE
// get the current time  - 2015-12-19 10:10:54
$current = Carbon::now();
$current = new Carbon();

// get today - 2015-12-19 00:00:00
$today = Carbon::today();

// get yesterday - 2015-12-18 00:00:00
$yesterday = Carbon::yesterday();

// get tomorrow - 2015-12-20 00:00:00
$tomorrow = Carbon::tomorrow();

// parse a specific string - 2016-01-01 00:00:00
$newYear = new Carbon('first day of January 2016');

// set a specific timezone - 2016-01-01 00:00:00
$newYearPST = new Carbon('first day of January 2016', 'America\Pacific');


// Blade View:
{{ \Carbon\Carbon::parse($registration->application_date)->toDayDateTimeString() }}