Easily Manage Weekday Start and End with Carbon in Laravel

0

 How to set start day and end day of week using Carbon Laravel

Set Start 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
  }
}



Post a Comment

0Comments
Post a Comment (0)