1myb
1myb

Reputation: 3596

Create the same time diff from a given datetime

I find the different of this two time with the following:

$expiry = new Carbon('Mar 21, 2019 17:40:00');
$create = new Carbon('2019-03-12 07:00:00');
$expiry->diffInSeconds($create);

and I want to create the new expiry with the same time range from today. The expiry need to be in exact same format.

$new_expiry = new Carbon(date("Y-m-d H:i:s", time()));
$new_expiry->addSeconds($sec)->format('M d,Y H:i:s');

I'm getting wrong new_expiry, any better code to do this?

Upvotes: 0

Views: 32

Answers (1)

thisiskelvin
thisiskelvin

Reputation: 4202

You should use the ::now() method in Carbon to get the current date and time instance.

$expiry = Carbon::createFromFormat('M d,Y H:i:s', 'Mar 21, 2019 17:40:00');
$create = Carbon::create(2019, 3, 12, 7, 0, 0);
$secs   = $expiry->diffInSeconds($create);

$new_expiry = Carbon::now()->addSeconds($secs)->format('M d,Y H:i:s');

Upvotes: 1

Related Questions