I will give an example of using Laravel's Carbon library to get the day from a date. You can use any laravel version of Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10.
use Carbon\Carbon; // Create a new Carbon instance from a date string $dateString = '2022-03-07'; $carbon = new Carbon($dateString); // Get the day of the week as a number (0 for Sunday, 6 for Saturday) $dayOfWeek = $carbon->dayOfWeek; // Get the day of the month as a number (1-31) $dayOfMonth = $carbon->day; // Get the day of the year as a number (1-365) $dayOfYear = $carbon->dayOfYear; // You can also get the day name as a string using the format() method $dayName = $carbon->format('l'); // "Monday" // Output the results echo "Day of the week: $dayOfWeek\n"; echo "Day of the month: $dayOfMonth\n"; echo "Day of the year: $dayOfYear\n"; echo "Day name: $dayName\n";
This code will output the following:
Day of the week: 1 Day of the month: 7 Day of the year: 66 Day name: Monday
Tags : laravel Laravel Carbon