Skip to content

Instantly share code, notes, and snippets.

@bobbybouwmann
Last active May 31, 2024 03:04
Show Gist options
  • Save bobbybouwmann/dbbedc46a5d87b37300e00c9ec0adb02 to your computer and use it in GitHub Desktop.
Save bobbybouwmann/dbbedc46a5d87b37300e00c9ec0adb02 to your computer and use it in GitHub Desktop.
Laravel Absolute vs Relative Dates with Carbon
<?php
use Carbon\Carbon;
// Create a new Carbon instance with the current timestamp
$date = Carbon::now();
// Displays: 15-1-2017
dump($date->format('d-m-Y'));
// Displays: 15-1-2017 18:56:05
dump($date->format('d-m-Y H:i:s'));
// Displays: Sun, 2017 Jan 15 18:56:05
dump($date->format('D, Y M j H:i:s'));
// Displays: Sunday 15th of January 2017 06:56:05 PM
dump($date->format('l jS \\of F Y h:i:s A'));
<?php
use Carbon\Carbon;
// Create a new Carbon instance with the current timestamp
$date = Carbon::now();
// Displays: Jan 15, 2017
dump($date->toFormattedDateString());
// Displays: Sun, Jan 15, 2017 06:56 PM
dump($date->toDayDateTimeString());
// Displays: Sunday, 15-Jan-2017 18:56:05 UTC
dump($date->toCookieString());
// Displays: Sun, 15 Jan 2017 18:56:05 +0000
dump($date->toRssString());
// Displays: 2017-01-15T18:56:05+00:00
dump($date->toIso8601String());
<?php
use Carbon\Carbon;
// Create a new Carbon instance with the current timestamp
$date = Carbon::now();
// Displays: UTC
dump($date->timezoneName);
// Displays: America/Vancouver
dump($date->setTimezone('America/Vancouver')->timezoneName);
// Displays: Sun, Jan 15, 2017 6:56 PM
dump($date->setTimezone('Europe/London')->toDayDateTimeString());
// Displays: Sun, Jan 15, 2017 10:56 AM
dump($date->setTimezone('America/Vancouver')->toDayDateTimeString());
<?php
use Carbon\Carbon;
// Create a new Carbon instance with some older date
$date = Carbon::createFromDate(1994, 11, 5);
// Displays: 22 years, 2 months and 10 days
dump($date->diff(Carbon::now())->format('%y years, %m months and %d days'));
// Displays: 22 years, 2 months and 10 days
dump(age($date));
// Displays: 22 years old
dump(age(new Carbon('22 years ago')));
// Displays: 22 years and 30 days old
dump(age(new Carbon('22 years, 30 days ago')));
// Displays: "22 years and 3 months old
dump(age(new Carbon('22 years, 3 months ago')));
// Displays: 22 years, 4 months and 10 days old
dump(age(new Carbon('22 years, 132 days ago')));
function age(Carbon $born)
{
// Get the current date
$now = Carbon::now();
// Get the diff between the two dates, this will calculate the diff
// in years, months and weeks
$diff = $now->diff($born);
// Determine if we have any days and if so add them to the string
$age = ($days = $diff->d) ? ' and ' . $days . ' ' . str_plural('day', $days) : '';
// Determine if we have any months and if so add them to the string
$age = ($months = $diff->m) ? ($age ? ', ' : ' and ') . $months . ' ' . str_plural('month', $months) . $age : $age;
// Determine if we have any years and if so add them to the string
$age = ($years = $diff->y) ? $years . ' ' . str_plural('year', $years) . $age : $age;
// Return the age string and clean up and redundant parts
return trim(trim($age, ', '), ' and ') . ' old';
}
<?php
use Carbon\Carbon;
// Displays: 5 days ago
dump(Carbon::now()->subDays(5)->diffForHumans());
// Displays: 5 days from now
dump(Carbon::now()->addDays(5)->diffForHumans());
// Displays: 1 week ago
dump(Carbon::now()->subDays(10)->diffForHumans());
// Displays: 2 years ago
dump(Carbon::now()->subDays(830)->diffForHumans());
// Displays: 12 minutes ago
dump(Carbon::now()->subMinutes(12)->diffForHumans());
// Displays: 1 hour ago
dump(Carbon::now()->subHour(1)->subMinutes(12)->diffForHumans());
// Displays: 48 hours
dump(Carbon::now()->subDays(2)->diffInHours() . ' hours');
// Displays: 0 days
dump(Carbon::now()->subHours(3)->diffInDays() . ' days');
// Display: 1 day
dump(Carbon::now()->subHours(30)->diffInDays() . ' day');
// Display: 86 day
dump(Carbon::now()->subMonth(2)->subWeek(3)->subDays(4)->diffInDays() . ' days');
<?php
use Carbon\Carbon;
// Create a new Carbon instance with the current timestamp
$date = Carbon::now();
// Displays: 5 days ago
dump($date->subDays(5)->diffForHumans());
// Displays: 1 second ago
dump($date->addDays(10)->diffForHumans());
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'age',
];
/**
* Get the age as a string of the user.
*
* @param $value
* @return string
*/
public function getAgeAttribute($value)
{
// Parse the value to a Carbon instance
// The value is probably something like 1994-11-05 in your database
// The parse method will create a Carbon instance based on the date
$born = Carbon::parse($value);
$now = Carbon::now();
// Get the diff between the two dates, this will calculate the diff
// in years, months and weeks
$diff = $now->diff($born);
// Determine if we have any days and if so add them to the string
$age = ($days = $diff->d) ? ' and ' . $days . ' ' . str_plural('day', $days) : '';
// Determine if we have any months and if so add them to the string
$age = ($months = $diff->m) ? ($age ? ', ' : ' and ') . $months . ' ' . str_plural('month', $months) . $age : $age;
// Determine if we have any years and if so add them to the string
$age = ($years = $diff->y) ? $years . ' ' . str_plural('year', $years) . $age : $age;
// Return the age string and clean up and redundant parts
return trim(trim($age, ', '), ' and ') . ' old';
}
}
@sribna
Copy link

sribna commented Apr 20, 2020

Relative dates, Lara 6, IMHO more readable version

use Illuminate\Support\Str;

/**
 * @param Carbon $date
 * @param null|Carbon $from
 * @return string
 */
function format_date_relative(Carbon $date, $from = null)
{
    if (!isset($from)) {
        $from = now();
    }

    $diff = $from->diff($date);
    $parts[] = $diff->y ? $diff->y . ' ' . Str::plural('year', $diff->y) : '';
    $parts[] = $diff->m ? $diff->m . ' ' . Str::plural('month', $diff->m) : '';
    $parts[] = $diff->d ? $diff->d . ' ' . Str::plural('day', $diff->d) : '';
    $parts[] = $diff->h ? $diff->h . ' ' . Str::plural('hour', $diff->h) : '';
    $parts[] = $diff->i ? $diff->i . ' ' . Str::plural('minute', $diff->i) : '';
    $parts[] = $diff->s ? $diff->s . ' ' . Str::plural('second', $diff->s) : '';

    if ($parts = array_filter($parts)) {
        $formatted = implode(' ', $parts);
        return $diff->invert ? "$formatted ago" : $formatted;
    }

    return "now";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment