mysql - Laravel - 时差

标签 mysql laravel

欢迎!我有问题想问你。我正在 Laravel 5.2 中为自己编写飞行日志。在公式中,我将有起飞时间和到达时间,但我不知道如何在 laravel 中计算时间差并将其自动传递到数据库。因此,最简单的方法是计算此时间差,例如:takoeff - 12 UTC 到达 - 14UTC,然后将此时间差保存到数据库中,即飞行时间 = 2。

谢谢你的帮助

最佳答案

Laravel 附带了 Carbon,它为此提供了一个简单的帮助器:

$departure = Carbon::parse($myTakeoffTimestamp); // 12 UTC
$arrival = Carbon::parse($myArrivalTimestamp); // 14 UTC

$diff = $departure->diffInHours($arrival, false); // 2

用于存储此数据:

Schema::create('flights', function(Blueprint $table){
    //other table stuff...

    $table->timestamp('departure');
    $table->timestamp('arrival');
    $table->timestamp('duration');
});

然后你可以在 Controller 中执行此操作:

public function store(Illuminate\Http\Request $request) 
{
    $flight = new Flight;
    $flight->unguard();
    $flight->create([
        //your other flight info
        'departure' = Carbon::parse($myTakeoffTimestamp),
        'arrival' => Carbon::parse($myArrivalTimestamp),
        'duration' => $takeoff->diffInHours($arrival)
    ]);
}

由于 Schema builder 中的 timestamp() 定义与 Carbon 的默认格式(即时间戳)完美配合,因此使用此方法无需额外工作即可将其存入数据库。

关于mysql - Laravel - 时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41249593/

相关文章:

php - 亡灵开关 : drop all tables from a database with php?

mysql - 在选择标签中显示当前用户的数据及其名称

MySQL sum() 来自多个表

php : The term 'php' is not recognized as the name of a cmdlet, 函数、脚本文件或可运行程序

laravel - 使用模型函数从查询中查出结果

html - 使用路由参数返回不完整且没有 Css 的 View [Laravel 5]

javascript - 在 Laravel 中获取基于 Ajax 的数据

php - MYSQL:为查询选择了不正确的排序规则

mysql - fedora 上 mariadb 的默认密码是多少?

php - 如何在 Laravel PHP 中使用控制台内核?