如何在 Laravel 中使用 Carbon 获得时间差
How to get difference in time using Carbon in Laravel
我想得到现在和我从数据库中获取的 subscription_endtime 之间的时间差。我正在使用以下代码
$subscription_endtime = DB::table('subscriptions')->where('user_id', $user_id)->value('subscription_endtime');
$today = Carbon::now();
$totalDuration = $subscription_endtime->diffInSeconds($today);
return $totalDuration;
我收到以下错误
"Call to a member function diffInSeconds() on string".
如何解决这个错误?
看起来 subscription_endtime 不是碳物体。你可以从 Carbon
解析它
$subscription_endtime = DB::table('subscriptions')->where('user_id', $user_id)->value('subscription_endtime');
$subscription_endtime = Carbon::parse($subscription_endtime); // add this line
$today = Carbon::now();
$totalDuration = $subscription_endtime->diffInSeconds($today);
return $totalDuration;
$today = Carbon::now(); //returns todays date and time at this particular second
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $subscription_endtime);//returns the time fetched from the database as a Carbon Object
$diff_in_seconds = $today->diffInSeconds($from);//returns the difference between $today and $from in seconds
print_r($diff_in_seconds );//prints the difference
我想得到现在和我从数据库中获取的 subscription_endtime 之间的时间差。我正在使用以下代码
$subscription_endtime = DB::table('subscriptions')->where('user_id', $user_id)->value('subscription_endtime');
$today = Carbon::now();
$totalDuration = $subscription_endtime->diffInSeconds($today);
return $totalDuration;
我收到以下错误
"Call to a member function diffInSeconds() on string".
如何解决这个错误?
看起来 subscription_endtime 不是碳物体。你可以从 Carbon
解析它$subscription_endtime = DB::table('subscriptions')->where('user_id', $user_id)->value('subscription_endtime');
$subscription_endtime = Carbon::parse($subscription_endtime); // add this line
$today = Carbon::now();
$totalDuration = $subscription_endtime->diffInSeconds($today);
return $totalDuration;
$today = Carbon::now(); //returns todays date and time at this particular second
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $subscription_endtime);//returns the time fetched from the database as a Carbon Object
$diff_in_seconds = $today->diffInSeconds($from);//returns the difference between $today and $from in seconds
print_r($diff_in_seconds );//prints the difference