Debug School

rakesh kumar
rakesh kumar

Posted on

Laravel Date and Time Method

Getting current date, time, and day in Laravel
Laravel Date and Time Methods
Use of Laravel Carbon
Laravel Error "Class 'App\Http\Controllers\DateTime' not found"
Getting a Specific Date and Time
Creating Dates with More Fine-Grained Control
Manipulating the Date and Time
date time Command
Formatting Date and Time
Calculating Relative Time

Getting current date, time, and day in Laravel

$my=Carbon::now();
$my->toDateTimeString();
Enter fullscreen mode Exit fullscreen mode

This will output in the usual format of Y-m-d H:i:s, there are many pre-created formats and you will unlikely need to mess with PHP date time strings again with Carbon.

Use Carbon::now()->format('d-m-Y')
Enter fullscreen mode Exit fullscreen mode
Try this,

$ldate = date('Y-m-d H:i:s');
Enter fullscreen mode Exit fullscreen mode
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
Enter fullscreen mode Exit fullscreen mode

click here
click here
click here

Laravel Date and Time Methods

Use of Laravel Carbon

click here

Laravel Error "Class 'App\Http\Controllers\DateTime' not found"

use DateTime;
Enter fullscreen mode Exit fullscreen mode

Getting a Specific Date and Time

Get the current time:

$current = Carbon::now();
Current time can also be retrieved with this instantiation:

$current2 = new Carbon();
Get today’s date:

$today = Carbon::today();
Get yesterday’s date:

$yesterday = Carbon::yesterday();
Get tomorrow’s date:

$tomorrow = Carbon::tomorrow();
Parse a specific string:

$newYear = new Carbon('first day of January 2016');
This returns:

Output
2016-01-01 00:00:00
These helpers provide human-readable requests for frequent date and time needs like today(), yesterday(), and tomorrow().
Enter fullscreen mode Exit fullscreen mode

click here

Creating Dates with More Fine-Grained Control

In addition to the quick ways to define date and times, Carbon also let’s us create date and times from a specific number of arguments.

createFromDate() accepts $year, $month, $day, $tz (time zone):

Carbon::createFromDate($year, $month, $day, $tz);
createFromTime() accepts $hour, $minute, $second, and $tz (time zone):

Carbon::createFromTime($hour, $minute, $second, $tz);
create() accepts $year, $month, $day, $hour, $minute, $second, $tz (time zone):

Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);

Enter fullscreen mode Exit fullscreen mode

click here

Manipulating the Date and Time

Grabbing the date and time isn’t the only thing you’ll need to do when working with dates. You’ll often need to manipulate the date or time.

For instance, when creating a trial period for a user, you will want the trial period to expire after a certain amount of time. So let’s say we have a 30-day trial period. We could calculate that time with Carbon’s add and subtract.

For this example, we can use addDays() to determine when the trial expires:

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

// add 30 days to the current time
$trialExpires = $current->addDays(30);
From the Carbon documentation, here are some of the other add() and sub() methods available to us.

Consider a date set to January 31, 2012:

$dt = Carbon::create(2012, 1, 31, 0);

echo $dt->toDateTimeString();
This will return:

Output
2012-01-31 00:00:00
Enter fullscreen mode Exit fullscreen mode

click here

date time Command

Command Output
echo $dt->addYear();    2012-01-31 00:00:00
echo $dt->addYears(5);  2017-01-31 00:00:00
echo $dt->subYear();    2011-01-31 00:00:00
echo $dt->subYears(5);  2007-01-31 00:00:00
Modifying the date with addMonths() and subMonths() will result in the following:

Command Output
echo $dt->addMonth();   2012-03-03 00:00:00
echo $dt->addMonths(60);    2017-01-31 00:00:00
echo $dt->subMonth();   2011-12-31 00:00:00
echo $dt->subMonths(60);    2007-01-31 00:00:00
Take note of how adding one month to “January 31” resulted in “March 3” instead of “February 28”. If you prefer to not have that rollover, you can use addMonthWithoutOverflow().

Modifying the date with addDays() and subDays() will result in the following:

Command Output
echo $dt->addDay(); 2012-02-01 00:00:00
echo $dt->addDays(29);  2012-02-29 00:00:00
echo $dt->subDay(); 2012-01-30 00:00:00
echo $dt->subDays(29);  2012-01-02 00:00:00
Modifying the date with addWeekdays() and subWeekdays() will result in the following:

Command Output
echo $dt->addWeekday(); 2012-02-01 00:00:00
echo $dt->addWeekdays(4);   2012-02-06 00:00:00
echo $dt->subWeekday(); 2012-01-30 00:00:00
echo $dt->subWeekdays(4);   2012-01-25 00:00:00
Modifying the date with addWeeks() and subWeeks() will result in the following:

Command Output
echo $dt->addWeek();    2012-02-07 00:00:00
echo $dt->addWeeks(3);  2012-02-21 00:00:00
echo $dt->subWeek();    2012-01-24 00:00:00
echo $dt->subWeeks(3);  2012-01-10 00:00:00
Modifying the date with addHours() and subHours() will result in the following:

Command Output
echo $dt->addHour();    2012-01-31 01:00:00
echo $dt->addHours(24); 2012-02-01 00:00:00
echo $dt->subHour();    2012-01-30 23:00:00
echo $dt->subHours(24); 2012-01-30 00:00:00
Modifying the date with addMinutes() and subMinutes() will result in the following:

Command Output
echo $dt->addMinute();  2012-01-31 00:01:00
echo $dt->addMinutes(61);   2012-01-31 01:01:00
echo $dt->subMinute();  2012-01-30 23:59:00
echo $dt->subMinutes(61);   2012-01-30 22:59:00
Modifying the date with addSeconds() and subSeconds() will result in the following:

Command Output
echo $dt->addSecond();  2012-01-31 00:00:01
echo $dt->addSeconds(61);   2012-01-31 00:01:01
echo $dt->subSecond();  2012-01-30 23:59:59
echo $dt->subSeconds(61);   2012-01-30 23:58:59
Using Carbon’s add and subtract tools can provide you with adjusted date and times.
Enter fullscreen mode Exit fullscreen mode

click here

Formatting Date and Time


PHP’s toXXXString() methods are available to display dates and times with predefined formatting:

Command Output
echo $dt->toDateString();   2015-04-21
echo $dt->toFormattedDateString();  Apr 21, 2015
echo $dt->toTimeString();   22:32:05
echo $dt->toDateTimeString();   2015-04-21 22:32:05
echo $dt->toDayDateTimeString();    Tue, Apr 21, 2015 10:32 PM
It’s also possible to use PHP’s DateTime format() for custom formatting:

echo $dt->format('l jS \of F Y h:i:s A');
l: A full textual representation of the day of the week.
jS:
Day of the month without leading zeros.
English ordinal suffix for the day of the month, 2 characters.
F: A full textual representation of a month.
Y: A full numeric representation of a year, 4 digits.
h:i:s:
12-hour format of an hour with leading zeros.
Minutes with leading zeros.
Seconds with leading zeros.
A: Uppercase Ante meridiem and Post meridiem.
This code will produce the following result:

Output
Tuesday 21st of April 2015 10:32:05 PM
Enter fullscreen mode Exit fullscreen mode

click here

Calculating Relative Time

Carbon also lets us display time relatively with the diff() methods.

For instance, let’s say we have a blog and wanted to show a published time of 3 hours ago. We would be able to do that with these methods.

Finding the Difference
Consider the following example with a time in the future and a time in the past:

$dt      = Carbon::create(2012, 1, 31, 0);
$future  = Carbon::create(2012, 1, 31, 0);
$past    = Carbon::create(2012, 1, 31, 0);

$future  = $future->addHours(6);
$past    = $past->subHours(6);
Here are the results using diffInHours():

Command Output
echo $dt->diffInHours($future); 6
echo $dt->diffInHours($past);   6
Consider the following example with a date in the future and a date in the past:

$dt      = Carbon::create(2012, 1, 31, 0);
$future  = Carbon::create(2012, 1, 31, 0);
$past    = Carbon::create(2012, 1, 31, 0);

$future  = $future->addMonth();
$past    = $past->subMonths(2);
Here are the results using diffInDays():

Command Output
echo $dt->diffInDays($future);  31
echo $dt->diffInDays($past);    61
Displaying the Difference for Humans
Displaying time relatively can sometimes be more useful to readers than a date or timestamp.

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

The diffForHumans() method is used for calculating the difference and also converting it to a humanly readable format.

Consider the following example with a date in the future and a date in the past:

$dt      = Carbon::create(2012, 1, 31, 0);
$future  = Carbon::create(2012, 1, 31, 0);
$past    = Carbon::create(2012, 1, 31, 0);

$future  = $future->addMonth();
$past    = $past->subMonth();
Here are the results using diffForHumans():

Command Output
echo $dt->diffForHumans($future);   1 month before
echo $dt->diffForHumans($past); 1 month after
Enter fullscreen mode Exit fullscreen mode

click here

Top comments (0)