Shirjeel Ahmed Khan
Shirjeel Ahmed Khan

Reputation: 267

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

Upvotes: 5

Views: 7413

Answers (2)

Milind Singh
Milind Singh

Reputation: 316

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');

Upvotes: 2

Marcus
Marcus

Reputation: 1848

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'
];

Upvotes: 10

Related Questions