Вычислите разницу во времени двух строк в laravel
Я хочу рассчитать время, различное между 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 / Calculate the time difference of two rows in laravel

27.01.2020 12:17:03
использовать UNIX_TIMESTAMP
$q = DB::table('users')->select(DB::raw("SUM(UNIX_TIMESTAMP(stime) - UNIX_TIMESTAMP(rtime)) as TIMESTAMPDIFF"))->get();
Является ответом!

27.01.2020 12:18:25
Используйте метод углерода, 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;
}

27.01.2020 12:18:29
$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);
Помочь в развитии проекта: