【问题标题】:Calculate the time difference of two rows in laravel在laravel中计算两行的时间差
【发布时间】:2020-05-12 15:17:12
【问题描述】:

我想计算 2 行之间的时间差异并计算排名。 这是我的代码:

 public function testtima($sid,$rid){
        $calcs=users::where([['si',$sid]])->first();
        $calcr=users::where([['ri',$rid]])->first();
        $stime=$calcs['created_at'];
        $rtime=$calcr['created_at'];
        $show['stime']=$stime;
        $show['rtime']=$rtime;
        $show['now']=carbon::now();
        return $show;
    }

我如何计算$rtime-$stime?

【问题讨论】:

标签: laravel


【解决方案1】:
$row1= "2007-03-24";
$row2= "2009-06-26";

$diff = abs(strtotime($row2) - strtotime($row1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

【讨论】:

    【解决方案2】:

    使用 Carbon 的方法,diffInSeconds、diffInDays 或 diffInMonths 等:

    public function testtima($sid,$rid){
        $stime = users::where('si',$sid)->first()->created_at;
        $rtime = users::where('ri',$rid)->first()->created_at;
        $diff = (new Carbon($stime))->diffInSeconds($rtime);
        //$diff = (new Carbon($stime))->diffInDays($rtime);   
        return $diff;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 1970-01-01
      • 2019-05-29
      • 2019-01-11
      • 2017-02-14
      • 2018-04-03
      • 2014-09-21
      • 1970-01-01
      相关资源
      最近更新 更多