Categories: Laravel Tutorial: Step by Step Guide to Building Your First Laravel Application - Techanical-Atom

Working With Date And Time In Laravel Carbon4 min read

Laravel Carbon
Reading Time: 3 minutes

Working With Date And Time In Laravel Carbon is very effective and efficient because of the use of the Carbon class which is instantiated from the date time class of the PHP. In this article we are going to cover some awesome and cool features of the date and time that you can use for your next Laravel Project. So let’s begin.

Working With Date And Time In Laravel Carbon

Date And Time In Laravel Carbon
Date And Time In Laravel Carbon

Unlike chemistry’s Carbon , the carbon in laravel has all of the functions inherited from the base DateTime class. This approach allows you to access the base functionality if you see anything missing in Carbon but is there in DateTime.To use Carbon in your project you have to use the following line in your controller’s namespace

use Carbon\Carbon;

# – Getting the Current Time and Date

Using the below written line of code will give you the current time of your timezone.You need to define the timezone of your area so get the correct time and date.or just pass the timezone as a string

$now = Carbon::now();

$nowInLondonTz = Carbon::now('Europe/London');

#- Formatting the Created At and Updated At

Laravel uses created at and updated at as default column while creating any table and they current timestamp set as the value but many times we need to have the time and date formatted according to your need use the following line of code for your beenfit

date('F d, Y',strtotime($variable->created_at)) at date('g:i a',strtotime($variable->created_at)) 

this will give you 06th June, 2018 at 1:09 PM which is more user friendly date format then the one stored in the database.

#- Comparison Among the dates

Simple comparison is offered up via the following functions. Remember that the comparison is done in the UTC timezone so things aren’t always as they seem.


echo Carbon::now()->tzName; // America/Toronto
$first = Carbon::create(2012, 9, 5, 23, 26, 11);
$second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');
echo $first->toDateTimeString(); // 2012-09-05 23:26:11
echo $first->tzName; // America/Toronto
echo $second->toDateTimeString(); // 2012-09-05 20:26:11
echo $second->tzName; // America/Vancouver
var_dump($first->eq($second)); // bool(true)
var_dump($first->ne($second)); // bool(false)
var_dump($first->gt($second)); // bool(false)
var_dump($first->gte($second)); // bool(true)
var_dump($first->lt($second)); // bool(false)
var_dump($first->lte($second)); // bool(true)
$first->setDateTime(2012, 1, 1, 0, 0, 0);
$second->setDateTime(2012, 1, 1, 0, 0, 0); // Remember tz is 'America/Vancouver'
var_dump($first->eq($second)); // bool(false)
var_dump($first->ne($second)); // bool(true)
var_dump($first->gt($second)); // bool(false)
var_dump($first->gte($second)); // bool(false)
var_dump($first->lt($second)); // bool(true)
var_dump($first->lte($second)); // bool(true)
var_dump($first->eq($second)); // bool(false)
var_dump($first->equalTo($second)); // bool(false)
var_dump($first == $second); // bool(false)
var_dump($first->ne($second)); // bool(true)
var_dump($first->notEqualTo($second)); // bool(true)
var_dump($first != $second); // bool(true)
var_dump($first->gt($second)); // bool(false)
var_dump($first->greaterThan($second)); // bool(false)
var_dump($first > $second); // bool(false)
var_dump($first->gte($second)); // bool(false)
var_dump($first->greaterThanOrEqualTo($second)); // bool(false)
var_dump($first >= $second); // bool(false)
var_dump($first->lt($second)); // bool(true)
var_dump($first->lessThan($second)); // bool(true)
var_dump($first < $second); // bool(true) var_dump($first->lte($second)); // bool(true)
var_dump($first->lessThanOrEqualTo($second)); // bool(true)
var_dump($first <= $second); // bool(true)

More :- What Is New In Laravel 5.6 Complete Guide

Displaying the Difference for Humans

Displaying time relatively has become popular in the past few years. This can be seen across the social networks like Twitter and Facebook.

For example, instead of displaying the time of a post like 8:12am, the time will be displayed as 3 hrs.

These methods are used for calculating the difference and also converting it to a humanly readable format.

There are four different possibilities for how to display times like this:

  • When comparing a value in the past to default now:
    • 1 hour ago
    • 5 months ago
  • When comparing a value in the future to default now:
    • 1 hour from now
    • 5 months from now
  • When comparing a value in the past to another value:
    • 1 hour before
    • 5 months before
  • When comparing a value in the future to another value:
    • 1 hour after
    • 5 months after

#Conclusion

There’s plenty more that Carbon can do. Be sure to look through the official Carbon docs. Hopefully this helps use date/times easier in PHP and speeds up development times! Keep visiting Laravel-Daily, Your Larvel News Source

 

All Rights Reserved @ Techanical-Atom
%d bloggers like this: