1

一定期間 (毎年、毎月など) を指定できるメソッドを書きたいと思います。指定された期間に応じて、前の日付が返されます。

これが私のコードです:

public function callRuleCeilling($period)
    {
        $start = new \DateTime();

        switch ($period) {
            case 'weekly':
                $dateInterval = 'P7D';
                break;
            case 'monthly':
                $dateInterval = 'P1M';
                break;
            case 'quaterly':
                $dateInterval = 'P3M';
                break;
            case 'half-yearly':
                $dateInterval = 'P6M';
                break;
            case 'yearly':
                $dateInterval = 'P1Y';
                break;
            default:
                $dateInterval = 'P1Y';
        }
        $start->sub(new \DateInterval($dateInterval));   

        return $start    
    }

私の例の問題:

開始日を年の途中に入れ、年間期間を設定した場合。年末年始でやめてほしい。

また、月間も同様にお願いしたい(月初ストップ)など...

それを行うPHP関数は存在しますか?見つからない。

私を強調してください。

4

1 に答える 1

0

ハイライトありがとう。それは私がそのようにすることを可能にしました:

public function callRuleCeilling($period)
    {

        $start = new \DateTime();
        $month = 'January';

        switch ($period) {
            case 'weekly':
                $timestampMonday = strtotime('last monday', strtotime('tomorrow'));
                $start = $start->setTimestamp($timestampMonday);
                break;
            case 'monthly':
                $month = $start->format('F');
                $start = new \DateTime('first day of '.$month);
                break;
            case 'quaterly':
                $monthNumber = $start->format('n');
                if($monthNumber >= 1) $month = 'January';
                if($monthNumber >= 5) $month = 'May';
                if($monthNumber >= 9) $month = 'September';
                $start = new \DateTime('first day of '.$month);
                break;
            case 'half-yearly':
                $monthNumber = $start->format('n');
                if($monthNumber >= 1) $month = 'January';
                if($monthNumber >= 7) $month = 'July';
                $start = new \DateTime('first day of '.$month);
                break;
            case 'yearly':
                $start = new \DateTime('first day of January');
                break;
            default:
                $start = new \DateTime('first day of January');
        }

        return $start;
    }
于 2015-09-23T14:34:02.170 に答える