I have made this method for sending a temporary code to user’s mobile phone number for verifying his account:
public function signInWithToken()
{
if(Session::has('nextStep') && Session::has('foundUser')) {
$user = User::where('usr_mobile_phone', Session::get('foundUser'))->first();
if(Session::has('codeSent')){
$nowTime = now();
$sentTime = Session::get('codeSent');
if($nowTime >= $sentTime){
$user->activeCode()->delete();
Session::forget('codeSent');
}
}else{
$code = ActiveCode::generateCode($user);
$user->notify(new ActiveCodeNotification($code, Session::get('foundUser')));
Session::put('codeSent', now()->addMinutes(10));
}
return view('frontend.auth.token');
}else{
abort(404);
}
}
So for the first time, a code generates and the session codeSent
will also be submitted and its value is 10 minutes above the current time.
Then I also need to check if this session was already submitted, then it has to subtract the $nowTime
from $sentTime
to find out if the code was generated more than 10 minutes ago and is too old: (so the code must be deleted from the DB and the session must be forgotten)
$nowTime = now();
$sentTime = Session::get('codeSent');
if($nowTime >= $sentTime){
$user->activeCode()->delete();
Session::forget('codeSent');
}
But this code won’t run and return this error:
Unsupported operand types: IlluminateSupportCarbon – IlluminateSupportCarbon
Value of nowTime
var:
Value of sentTime
var (set the currentTime + 10 minutes as the expire time of session codeSent
):
And I know this is because those sentTime
&& nowTime
variable values can not be subtracted from eachother somehow.
But really don’t know what is the correct way for doing this check!
So if you know, please let me know, I would really appreciate any idea or suggestion from you guys…
2
Answers
you can use Carbon diffInSeconds like the code below(untested)
first you need to parse times with carbon
then use this:
If you need to find the difference in minutes
You can use it: