Home > Software engineering >  How to compare twoTimestamps in Laravel?
How to compare twoTimestamps in Laravel?

Time:11-24

Hello i want to ask laravel date compare. please telme this code is right. I want to check before login.

  1. When user Registered, user must be deactivated
  2. Admin must activate: end_date=activated_date 90 days
  3. Admin Deactive : 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); }

CodePudding user response:

If do you want to check if $first is before or after $second

$first->isBefore($second);

or

$first->isAfter($second);

And if do you need to have diff, you can use

$first->diffInSeconds($second);
$first->diffInHours($second);

CodePudding user response:

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'
    ];
*/
  • Related