dpda53918 2015-09-23 01:32
浏览 63
已采纳

PHP DateInterval。 回到年初

I would like to write a method which I can give a period of time (Like: yearly, monthly...) and it returns me an anterior date according to this period of time given.

Here is my code:

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    
    }

My example problem:

If I put a starting date in the middle of the year with a yearly period. I want it to stop at the beginning of the year.

And I would like the same for monthly period (Stop at the beginning of the month) etc...

Does it exist a PHP function with do that? I can't find it.

Please highlight me.

展开全部

  • 写回答

1条回答 默认 最新

  • dsq1982 2015-09-23 06:34
    关注

    Thanks fo the highlight. It allowed me to did that way:

    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;
        }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部