dpda53918 2015-09-23 09: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 14: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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c