Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sharim's avatar

Apply Functions according to the current day using Carbon

I'm trying to add some functions inside if() condition and check what is the day today something like this.

$today = Carbon::now();
$monday = $request->day;
$thursday = $request->day2;
if($today->between($monday, $thursday)
{
$status = 'Today is between Monday to Thursday';
}
else {
$status = 'Today is Between Friday to Sunday.';
}

It was working for me yesterday(Monday). But it's not working Today (Tuesday).

0 likes
29 replies
Sinnbeck's avatar

What is the output of dd($monday, $thursday); ?

Sharim's avatar

@Sinnbeck I'm calling it from database. It's Monday, Thursday. even if I do like this:

if(Carbon::now()->between('Monday', 'Thursday'))

Its only working for Monday & Thursday. Not for the day between them.

Sinnbeck's avatar

@Sharim It will treat monday as next monday. Try dd(Carbon::parse('Monday')->toDateString());. Use dd(now()->weekday(1)->toDateString());

Sharim's avatar

@Sinnbeck Yeah its giving me "2022-10-31". The next Monday. But how can I add count only the days not the dates.

Sinnbeck's avatar

@Sharim You need to tell it what week we are taking about. Eg. dd(now()->weekday(1)->toDateString()); (1 = monday)

Sharim's avatar

@Sinnbeck I need to convert string values into boolean.

But I have only 2 days stored in database. 'Monday' & 'Thursday'. I don't know how can I convert all the days between them.

Sinnbeck's avatar

@Sharim That is not a boolean. Its 0 = Sunday, 1 = Monday, 2 = Tuesday etc. So just store an integer in the database instead

Or even add a lookup map

$map = [
'Sunday' => 1,
'Monday' => 1,
'Tuesday' => 2,
];
$start = now()->weekday($map[$monday]);
Sharim's avatar

@Sinnbeck Right now from the admin panel I'm storing days like this: <input type="text">.

Can I do something like this:

<select name="Weekdays">
<option name="Monday" value="1" />
<option name="Thursday" value="4" />
</select>
Sharim's avatar

@Sinnbeck That's great but how can I check the condition. Can I do like this?

if(now()->weekday(1)->toDateString()->between(1, 4))
Sinnbeck's avatar

@Sharim Just do this

$start = now()->weekday(1);
$end = now()->weekday(4);
if(now()->between($start, $end))
MichalOravec's avatar

@Sharim It's really difficult to read the documentation

if (in_array(now()->dayOfWeek, range(1, 4))) {
    // monday to thursday
}
1 like
Sharim's avatar

@MichalOravec if sunday = 0. Then Friday to Sunday if (in_array(now()->dayOfWeek, range(5, 0))) is not working.

Sharim's avatar

@Sinnbeck yes Sir I tried like this:

$start = now()->weekday(1);
$end = now()->weekday(4);
if(now()->between($start, $end))

its working but from Friday to Sunday:

$start = now()->weekday(5);
$end = now()->weekday(0);
if(now()->between($start, $end))

is not working.

Its counting Sunday to Friday. Instead of Friday to Sunday.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Sharim I assume you are coming from a place where sunday isnt the first day of the week?

You can probably set it globally (like setting your locale in your config/app.php) but

$start = now()->startOfWeek(\Carbon\Carbon::MONDAY)->weekday(1);
$end = now()->startOfWeek(\Carbon\Carbon::MONDAY)->weekday(4);
if(now()->between($start, $end)) {
Sharim's avatar

@Sinnbeck My config/app.php is something like this according to my location.

'timezone' => 'Asia/Dubai',
'locale' => 'en',
Sharim's avatar

@Sinnbeck I did like this in AppServiceProvider:

public function boot()
    {
        Carbon::endOfWeek(0);
    }

but its giving me this error while clearing the cache:

Modify to start of current hour, minutes and seconds become 0

Shivamyadav's avatar

@Sharim try this

$today = Carbon::now();
return $today->format('l'); //This is small alphabet L in the format fun.

Sharim's avatar

@Shivamyadav It will only convert integer value into the string day. But it will not check the days between monday to sunday.

Sharim's avatar

@Sinnbeck Ahh! Your answer above worked for me. I was just trying to fix it from config as recommended way. I'm Sorry to waste your time.

Sharim's avatar

@MichalOravec It can only count 2 days. 'Wednesday' & 'Friday'.

But I want to check all the days in the week. I tried it from the Carbon docs before but I couldn't do it.

Shivamyadav's avatar

@Sharim try this

$today = Carbon::now();
return $today->format('l'); //This is small alphabet L in the format fun.

Please or to participate in this conversation.