从 Laravel 中的时间戳字段计算

问题描述 投票:0回答:1

我有一个时间戳字段,其值类似于

2023-08-30 15:23:03
。现在我想计算这个值是否超过 2 分钟。

我怎样才能做到这一点?

laravel time timestamp
1个回答
1
投票

您可以使用 Laravel 中默认安装的 Carbon 包。要将时间添加两分钟,您需要使用 Carbon 中的 addMinutes 函数。您可以使用 subMinutes 函数来减少时间。

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

     }

注意时间的作用。首先,我们运行产品模型并从中读取数据并检查其制造时间并比较该时间是否小于当前时间,即增加 2 分钟。您可以反过来做,例如从活动时间中减去 2 分钟并进行比较。

上述代码的输出:

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
© www.soinside.com 2019 - 2024. All rights reserved.