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
860 stars 110 forks source link

how to get start of month #44

Closed Ali031 closed 5 years ago

morilog commented 6 years ago

Start week day of month? or what? please give me some description.

Ali031 commented 6 years ago

i mean how to find start day of current month in carbon is carbon::now()->startOfMonth();

morilog commented 6 years ago

There is not any function for startOfMonth directly. but you can use functions of jDateTime class for getting current month. BTW this lib is not for manipulating date time or something like carbon. the main functionality of this lib is converting Gregorian datetime to Jalali datetime.

amirhs712 commented 2 years ago

Hey there @morilog, sorry to bring up this closed issue but I think adding getStartOfMonth and getEndOfMonth would be beneficial for many people (including myself :) ).

However I wrote this code to use in my current project:

    public static function getStartOfMonth(Carbon $dt = null) : Jalalian
    {
        if (!$dt) {
            $dt = now();
        }

        $jdt = jdate($dt);

        return new Jalalian($jdt->getYear(), $jdt->getMonth(), 1, $jdt->getHour(), $jdt->getMinute(), $jdt->getSecond(), $jdt->getTimezone());
    }

    public static function getEndOfMonth(Carbon $dt = null) : Jalalian
    {
        if (!$dt) {
            $dt = now();
        }

        $jdt = jdate($dt);

        if ($jdt->isLeapYear() && $jdt->getMonth() === 12) {
            $lastDayOfCurrentMonth = 29;
        }else{
            $lastDayOfCurrentMonth = $jdt->getMonth() > 6 ? 30 : 31;
        }

        return new Jalalian($jdt->getYear(), $jdt->getMonth(), $lastDayOfCurrentMonth, $jdt->getHour(), $jdt->getMinute(), $jdt->getSecond(), $jdt->getTimezone());
    }

(I could also help with a PR)

morilog commented 2 years ago
 return new Jalalian($jdt->getYear(), $jdt->getMonth(), 1, $jdt->getHour(), $jdt->getMinute(), $jdt->getSecond(), $jdt->getTimezone());
    }

This is not the start of the month. the start hours, minutes and, seconds of month should be 00