How to set start day and end day of week using Carbon Laravel
In this article we will see how to set start day and end day of a week using carbon library in Laravel . As there are tons of methods provided by Carbon library but to set start day and end day of a week we will use startOfWeek() and endOfWeek() method .
Controller :
Write the following code on your controller to set start day of week and end day of a week .
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Carbon\Carbon; class UserController extends Controller { public function setStartEndWeek() { $current_date = Carbon::now(); // 2021-02-06 04:34:26 $set_start_of_week = $current_date->startOfWeek(Carbon::MONDAY); // 2021-02-01 00:00:00 $set_end_of_week = $current_date->endOfWeek(Carbon::SUNDAY); // 2021-02-07 23:59:59 return $set_end_of_week->toDateTimeString(); // 2021-02-07 23:59:59 } }
Thank you for reading this article 😊
For any query do not hesitate to comment 💬