morilog / jalali

This Package helps developers to easily work with Jalali (Shamsi or Iranian) dates in php appliations, based on Jalali (Shamsi) DateTime class.
MIT License
856 stars 111 forks source link

Request: The week of month method is needed #97

Closed thrashzone13 closed 3 years ago

thrashzone13 commented 4 years ago

Hi like carbon the weekOfMonth property is needed

morilog commented 4 years ago

Can you add the method and make a merge request?

thrashzone13 commented 4 years ago

Yes let me see if i can do it later on today.

thrashzone13 commented 4 years ago

I'm afraid I can't make a merge request. by the way I wrote the method

public function getWeekOfMonth(): int { $week = 1; for ($i = 1; $i <= $this->getMonthDays(); $i++) { if ($this->addDays($i)->isEndOfWeek()) $week++; if ($this->day == $i) return $week; } } I appreciate you add it if its correct

morilog commented 4 years ago

There is not any fear. you can write code and test. make MR when all tests pass. BTW the problem might be solved by bellow code: $weekOfMonth = (int) ceil($this->day / 7)

thrashzone13 commented 4 years ago

$weekOfMonth = (int) ceil($this->day / 7) doesn't work correctly as months can have 4, 5 or 6 weeks

hshahdoost commented 4 years ago

The following would get the job done

$offset = (new Jalalian($this->year, $this->month, 1))->getDayOfWeek();
$weekOfMonth = ceil(($offset + $this->day) / 7);
thrashzone13 commented 4 years ago

@hshahdoost It works fine tnx

morilog commented 4 years ago

@hshahdoost please make a merge request and add getWeekOfMonth() method