1

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.

Kevin
  • 4,823
  • 6
  • 36
  • 70
  • You need just to check what the interval between the given date and the start of the month/year is. Then compare them and return what you need. You may want to check Carbon by nesbot, it is cool library for date & time manipulation. – Konstantin Rachev Sep 23 '15 at 09:35
  • `if(time()>=strtotime($start)) echo "Time passed";` – 131 Sep 23 '15 at 09:39

1 Answers1

0

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;
    }
Kevin
  • 4,823
  • 6
  • 36
  • 70