laravel 用碳增加秒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53255684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:09:54  来源:igfitidea点击:

Adding seconds with Carbon

phplaraveldatetimestamp

提问by Младен Кари?

The idea is that I need to get end_date from the Auction via this model, which is inside a database, in MySQL timestamp format (I use phpmyadmin, if it matters, ex. 2018-11-14 04:58:07). So when I get the end_date, the idea is to increment it by few seconds (ex. 10 seconds) via Carbon addSeconds() function and then write it again into the DB. Here is the controller where it's done and my Auction model. What happens is that i get the FatalThrowableError 'Call to a member function addSeconds() on integer'. I played around quite a lot and can't seem to find right format for the secs2 variable.

这个想法是我需要通过这个模型从 Auction 中获取 end_date,该模型位于数据库中,采用 MySQL 时间戳格式(如果重要,我使用 phpmyadmin,例如 2018-11-14 04:58:07)。因此,当我获得 end_date 时,想法是通过 Carbon addSeconds() 函数将其增加几秒(例如 10 秒),然后再次将其写入数据库。这是完成的控制器和我的拍卖模型。发生的情况是我收到 FatalThrowableError 'Call to a member function addSeconds() on integer'。我玩了很多,似乎找不到 secs2 变量的正确格式。

Auction.php

拍卖.php

public $id;
public $name;
public $descript;
public $price;
public $pic;
public $end_date;
public $offers;
public $offered_by;

    public function get(){
    $result =
        DB::table('auctions')
            ->select('*')
            ->where('id', '=', $this->id)
            ->first();
    return $result;
}

    public function increment($id){
    $result =
        DB::table('auctions')
            ->where('id', $id)
            ->update([
                'offers' => DB::raw('offers + 1'),
                'end_date' => $this->end_date
            ]);
    return $result;
}

AuctionController.php

拍卖控制器.php

    public function offer($id, Request $request){
    $auction = new Auction();
    $auction->id = $id;

    $secs = $auction->get()->end_date;
    $secs2 = strtotime($secs);
    $secs2->addSeconds(120);

    $auction->end_date = $secs2;
    //dd($secs2);

    $auction->increment($id);
}

回答by Saengdaet

If you want to use addSecondsmethod, you can't convert your time into number first.. ex:

如果要使用addSeconds方法,则不能先将时间转换为数字。例如:

Carbon::parse($auction->get()->end_date)
      ->addSeconds(120)
      ->format('H:i:s');