Thursday, February 3, 2022

[FIXED] Cakephp how to get 1st and last datetime of a month

Issue

I am trying to get 1st and last datetime by FrozenTime

I have written below function

public function getFristAndLastDate($year,$month)
{
        $time = FrozenTime::now()->setDate((int)$year, (int)$month, 01)->i18nFormat('yyyy-MM-dd HH:mm:ss');

        return [
            $time->startOfMonth(),
            $time->endOfMonth()
        ];
}

After call function I am getting error

Call to a member function startOfMonth() on string

How can I get 1st and last datetime from year and month in cakephp using FrozenTime ?


Solution

Use create function to create a new ChronosInterface instance from a specific date and time.

FrozenTime::create((int) $year, (int) $month);


Answered By - Alimon Karim

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.