I have a timestamp field with value like 2023-08-30 15:23:03
. Now I would like to calculate whether this values is more than 2 minutes from now or not.
How can I do that ?
I have a timestamp field with value like 2023-08-30 15:23:03
. Now I would like to calculate whether this values is more than 2 minutes from now or not.
How can I do that ?
You can use the Carbon package that is installed by default in Laravel. To add two minutes to the time, you need to use the addMinutes function in Carbon. You can use the subMinutes function to reduce the time.
public function time()
{
$product = \App\Models\Product::first();
$now_time = Carbon::now();
return ($product->created_at > $now_time->addMinutes(2))
? 'Your time is less than 2 minutes more than the current time.'.'Time Now(add 2 Minute)= '.$now_time.' My Time = '.Carbon::now()
: 'Your time is more than 2 minutes longer than the current time.'.'Time Now(add 2 Minute)= '.$now_time.' My Time = '.Carbon::now();
}
Pay attention to the function of time. First, we run the product model and read a data from it and check its manufacturing time and compare whether this time is less than the current time or not, which adds 2 minutes to it. You can do it the other way around, say subtract 2 minutes from the active time and do the comparison.
The output of the above code:
Your time is more than 2 minutes longer than the current time.Time Now(add 2 Minute)= 2023-08-31 15:18:33 My Time = 2023-08-31 15:16:33