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).
What is the output of dd($monday, $thursday); ?
@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.
@Sharim It will treat monday as next monday. Try dd(Carbon::parse('Monday')->toDateString());. Use dd(now()->weekday(1)->toDateString());
@Sinnbeck Yeah its giving me "2022-10-31". The next Monday.
But how can I add count only the days not the dates.
@Sharim You need to tell it what week we are taking about. Eg. dd(now()->weekday(1)->toDateString()); (1 = monday)
@Sinnbeck So it can only identify the boolean values not the string?
@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.
@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]);
@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>
@Sinnbeck That's great but how can I check the condition. Can I do like this?
if(now()->weekday(1)->toDateString()->between(1, 4))
@Sharim Just do this
$start = now()->weekday(1);
$end = now()->weekday(4);
if(now()->between($start, $end))
@Sharim It's really difficult to read the documentation
if (in_array(now()->dayOfWeek, range(1, 4))) {
// monday to thursday
}
@MichalOravec if sunday = 0. Then Friday to Sunday if (in_array(now()->dayOfWeek, range(5, 0))) is not working.
@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.
@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)) {
@Sinnbeck My config/app.php is something like this according to my location.
'timezone' => 'Asia/Dubai',
'locale' => 'en',
@Sharim Try adding this to your AppServiceProvider
Carbon::endOfWeek(0);
@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
@Sharim try this
$today = Carbon::now();
return $today->format('l'); //This is small alphabet L in the format fun.
@Shivamyadav It will only convert integer value into the string day. But it will not check the days between monday to sunday.
@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 No worries :) Good that it works
@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.
@Sharim Start thinking man... it was just an example...
@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.