I don't see anything about weeksInMonth in the carbon docs (there is a daysInMonth). What kind of answer are you trying to get? How many complete weeks there are (Sunday-Saturday)? Like there are 3 complete 7 day weeks in Jan 2018, but also 2 partial weeks. Is the answer you are looking for 3, or 5 with that example?
Carbon get count of weeks in month
Hi, I tried make script for counting of weeks in month, but this is not work well . :(
$this->date = Carbon::createFromDate(2018,1);
print $this->date->weeksInMonth;
this code is not working :(
the total number of weeks in the month beginning on Monday
help for weeks - weekOfMonth sorry ...
You need to use weekOfMonth ;)
$dt = Carbon::parse('2012-9-5 23:26:11.123789');
var_dump($dt->weekOfMonth); // int(1)
I have not parse method in use ...
$this->month = Carbon::createFromDate(2018,1,1);
It doesn't matter what you use. As long as you start with a Carbon date
$month = Carbon::createFromDate(2018,1,1)->weekOfMonth;
if you just wanna know what is the current week of the month then as @bobbybouwmann said use weekOfMonth.
if you wanna get total number of weeks in a month then you need to create a function to calculate that because it's always almost not a whole number except for February.
//totally upto you, what to pass and how to handle it, easy is passing number of days in a month, and carbon does that very easily.
public function weeksInMonth($numOfDaysInMonth)
{
$daysInWeek = 7;
$result = $daysInMonth/$daysInWeek;
$numberOfFullWeeks = floor($result);
$numberOfRemaningDays = ($result - $numberOfFullWeeks)*7;
return 'Weeks: '.$numberOfFullWeeks.' - Days: '.$numberOfRemaningDays;
}
Now
$numberOfDaysInMonth= Carbon\Carbon::now()->daysInMonth;
$this->weeksInMonth($numberOfDaysInMonth);
Weeks: 4 - Days: 3 //for Jan
if you just want the weeks then just return that... this is just to give you an idea... cheers mate!
$month = Carbon::createFromDate(2018,1,1)->weekOfMonth;
it returns only number 1, this is not what I want ... Maybe count of weeks doesnt working. Better way will be my extra solution...
Set the monday start of week and next count all mondays in month. In for cycle go trough all 12 months in year and count all mondays in each month. First save first monday in current month in for cycle and add to array. Add week and this is second monday, add 2 week is third monday and 3 is fourth monday. Next save past monday in month ... whne the index 3 and 4 is the same value, one item of these two will be unset from array and i got it that what i want :)))) Working for all month, all days :))
Next ex..
$details['days']['mondays'][0] = Carbon::parse('first monday of'.$this->month'.
'2018')->format('j');
$details['days']['mondays'][1] = Carbon::parse('first monday of'.$this->month'.
'2018')->addWeek(1)->format('j');
$details['days']['mondays'][2] = Carbon::parse('first monday of'.$this->month'.
'2018')->addWeek(2)->format('j');
$details['days']['mondays'][3] = Carbon::parse('first monday of'.$this->month'.
'2018')->addWeek(3)->format('j');
$details['days']['mondays'][4] = Carbon::parse('last monday of'.$this->month'.
'2018')->format('j');
if( $details['days']['mondays'][3] == $details['days']['mondays']['4']){
unset($details['days']['mondays'][4]);
}
Example of output array, january 2018 mondays
Array
(
[mon] => January 2018
[days] => Array
(
[mondays] => Array
(
[0] => 1
[1] => 8
[2] => 15
[3] => 22
[4] => 29
)
)
)
You asked for the count in weeks and now you talk about the first day of each week... I'm done, I have no clue what you want man!!
Yes I asked, but your solution doesnt work ... and alternative work, count of weeks counting from the number of mondays well ...
Well the solution works as expected, but you wanted to have another answer on your question which is fine.
Did you answer your own question or can we help you with anything else?
I propose my alternative solution, but still I need to know how count number of weeks per month .. in ideal example:
- count of weeks per month
- and by each weeks start and end day
@vandyczech by default Carbon's week day starts from monday, saturdays and sundays are considered as weekends, you can check that by $date->isWeekDay() and $date->isWeekEnd() which returns true or false depending on the day.
i think you want something like this, didn't have much time, so see if it helps turn it into a function and done..,
Route::get('/enter_month_num/{month}',function($month){
$year = Carbon::now()->year;
$date = Carbon::createFromDate($year,$month);
$numberOfWeeks = floor($date->daysInMonth / Carbon::DAYS_PER_WEEK);
$start = [];
$end = [];
$j=1;
for ($i=1; $i <= $date->daysInMonth ; $i++) {
Carbon::createFromDate($year,$month,$i);
$start['Week: '.$j.' Start Date']= (array)Carbon::createFromDate($year,$month,$i)->startOfWeek()->toDateString();
$end['Week: '.$j.' End Date']= (array)Carbon::createFromDate($year,$month,$i)->endOfweek()->toDateString();
$i+=7;
$j++;
}
$result = array_merge($start,$end);
$result['numberOfWeeks'] = ["$numberOfWeeks"];
return $result;
});
if you hit : http://127.0.0.1:8000/enter_month_num/1
//result
{
"Week: 1 Start Date": [
"2018-01-01"
],
"Week: 2 Start Date": [
"2018-01-08"
],
"Week: 3 Start Date": [
"2018-01-15"
],
"Week: 4 Start Date": [
"2018-01-22"
],
"Week: 1 End Date": [
"2018-01-07"
],
"Week: 2 End Date": [
"2018-01-14"
],
"Week: 3 End Date": [
"2018-01-21"
],
"Week: 4 End Date": [
"2018-01-28"
],
"numberOfWeeks": [
"4"
]
}
if you change the month to 2: http://127.0.0.1:8000/enter_month_num/2
{
"Week: 1 Start Date": [
"2018-01-29"
],
"Week: 2 Start Date": [
"2018-02-05"
],
"Week: 3 Start Date": [
"2018-02-12"
],
"Week: 4 Start Date": [
"2018-02-19"
],
"Week: 1 End Date": [
"2018-02-04"
],
"Week: 2 End Date": [
"2018-02-11"
],
"Week: 3 End Date": [
"2018-02-18"
],
"Week: 4 End Date": [
"2018-02-25"
],
"numberOfWeeks": [
"4"
]
}
hope it helps...
Total weeks in a month: $date = Carbon::createFromDate(2022,1)->endOfMonth()->weekOfMonth; // 5
I hope this helps someone
Please or to participate in this conversation.