Ravisdo
Ravisdo

Reputation: 3

PHP Carbon - Check if today is even day of month

i must check if today is an even day in the month.

An Cronjob example would be "* * */2 * *" from CrontabGuru

But i dont know how to check this in PHP with Carbon.

$date = Carbon::now();

I think i must check the days of the month with the current day something like this

($date->daysInMonth / $date->day) % 2

Upvotes: 0

Views: 1235

Answers (2)

Jarrett Barnett
Jarrett Barnett

Reputation: 803

20 % 2 == 0 (even)

15 % 2 == 1 (odd)

if (!empty(($date->day % 2))) {
    // is odd
}

if (empty(($date->day % 2))) {
    // is even
}

or example method usage:

public function isEvenDay()
{
    return boolval($date->day % 2) === false;
}

public function isOddDay()
{
    return boolval($date->day % 2);
}

Upvotes: 0

dimooski
dimooski

Reputation: 36

Try something like this:

$evenDays = [
  2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
];

if (in_array($date->day, $evenDays)) {
  // Day is even 
}

Upvotes: 1

Related Questions