public function nextDate()
{
$lastDate = Carbon::now();
$lastDate = Carbon::parse('2021-04-21 00:00:00');
$eventDays = [3, 6]; // we only want wed and sat
$lastDay = $lastDate->dayOfWeek;
foreach ($eventDays as $eventDay) {
if ($lastDay < $eventDay) {
break; // found our next $eventDay
}
}
$nextDate = $lastDate->next($eventDay);
dd($nextDate); // date of next wed or sat
}
Using day number to get the next date
I love Carbon, but man, the documentation is vast, and overwhelming.
So I have saved in the database the numbers that represent two days. For example: One record has a string [3,6].
This means that for this record, dates associated with it will be on either a Wednesday or a Saturday. But the issue is finding the next actual date.
So lets say I have a record with the date 2021-04-21 00:00:00. I can do this to get what day that is:
Carbon::parse(2021-04-21 00:00:00)->dayOfWeek;
Since this evaluates to a 3, the second number will be the date to perform the next task.
My problem is this: I know the last recorded date, and can get the day number the date was on. But how do I use that date and the second number (6) to look forward to get that day's date?
Here is what I did that works....
public function getNextDrawDate($game)
{
// Get dame data based on machine_name ($game)
$game_data = Game::where('machine_name', $game)->first();
switch($game_data->lottery_results_table) {
case 'ntl_powerball_draws':
// Last recorded entry
$last_recorded_game = NtlPowerballDraws::latest()->first();
$last_recorded_game_draw_day = Carbon::parse($last_recorded_game->draw_date)->dayOfWeek;
if ($last_recorded_game_draw_day == 3) {
$day = 'Saturday';
} else if ($last_recorded_game_draw_day == 6) {
$day = 'Wednesday';
}
break;
case 'ntl_megamillions_draws':
// Last recorded entry
$last_recorded_game = NtlMegamillionsDraws::latest()->first();
$last_recorded_game_draw_day = Carbon::parse($last_recorded_game->draw_date)->dayOfWeek;
if ($last_recorded_game_draw_day == 2) {
$day = "Friday";
} else if ($last_recorded_game_draw_day == 5) {
$day = "Tuesday";
}
break;
}
return Carbon::parse($last_recorded_game->draw_date)->modify("next $day")->format('Y-m-d');
}
But I don't want to hard code the days. I want to use the numbers. I just don't know how.
Please or to participate in this conversation.