Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Add 30 minutes in field. Example: $is_expired = $created_at + 30minutes;

I want to add two timestamps, like in this example:

$created_at = "2018-07-23 12:15:43";
$is_expired = $created_at + 30mins;

The content of $is_expired should be 2018-07-23 12:45:43

like image 951
Shirjeel Ahmed Khan Avatar asked Jul 24 '18 05:07

Shirjeel Ahmed Khan


People also ask

How do you add 30 minutes to time in Laravel?

$is_expired = $created_at->addMinutes(30); Carbon is installed by default in Laravel and your dates should be automatically mutated by Laravel. Save this answer.

How to add minute for time in Laravel?

Using the carbon addMinute() or addMinutes() function you can change the minutes in the date in laravel 8. If we need to add minute or more then one minutes in date and time then you can use carbon in laravel. carbon provides addMinute() and addMinutes() method to add minutes on carbon date object.

How to add two time in Laravel?

Save this question. Show activity on this post. $in1 = explode(' ', "clock in time = 20 minutes"); $out1 = explode(' ', "clock out time = 10 minutes"); $start_time1 = $in1[4] .

What is time () in Laravel?

What is time () in Laravel? The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).


2 Answers

You can use core php functions:

//set timezone
date_default_timezone_set('GMT');

$date = new DateTime();
$created_at = $date->format('U = Y-m-d H:i:s');
$unixTimestamp = time() + 1800; // 30 * 60 

$date = new DateTime();
$date->setTimestamp($unixTimestamp);
$is_expired = $date->format('U = Y-m-d H:i:s');
like image 34
Milind Singh Avatar answered Oct 29 '22 13:10

Milind Singh


Using Carbon you can do

$is_expired = $created_at->addMinutes(30);

Carbon is installed by default in Laravel and your dates should be automatically mutated by Laravel.

If the date is not mutated then you can parse them to Carbon instance using Carbon::Parse($created_at)

Or if you have $dates = [] in your model you should add the created_at in it like so

protected $dates = [
    'created_at',
    'updated_at',
    'deleted_at'
];
like image 119
Marcus Avatar answered Oct 29 '22 14:10

Marcus