skip to Main Content

Hello i want to compare dates in laravel
please tell me this code is right.
I want to check it before login.

  1. When user is registered, user must be deactivated
  2. Admin must activate the user: end_date=activated_date +90 days
  3. Admin Deactivation : end_date=deactivated_date;
  4. if current_date=deactivate_date
if ($user->activated_at ->gte(now()->subDays(90)))   
{ 
   return $this->sendError('messages.user_subscription', [], 400); 
}

2

Answers


  1. You can check whether $first is before or after $second using:

    $first->isBefore($second);
    

    or vice versa:

    $first->isAfter($second);
    

    And if you need to get calculate difference, you can use below commands for difference in seconds and hours:

    $first->diffInSeconds($second);
    $first->diffInHours($second);
    
    Login or Signup to reply.
  2. Yes you code is correct, if it does not work you have to add activated_at in $dates in User model

    .....
    User extends Model 
    {
    ....
    protected $dates = ['activated_at'];
    ....
    /* Or
        protected $casts = [
            'activated_at' => 'datetime'
        ];
    */
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search