Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RonB1985's avatar

Carbon date range / intervals

Apologies for the title, not sure how to describe this, but hopefully a simple solution.

What I would like to achieve is the following. I have a start date in my database, for example the 01-01-2016. In another field in my database I set a monthly interval, which can be either 1, or 3, for example.

In my view I want to return a result based on these two fields. So for example, if the date is 01-01-2016 and the interval is 1 month, then on the first of every month I want to show a result. This can be done easily. But what if the interval is 3?

If the interval is 3, I want to show a result only on the 1st of january, then on the 1st of april, then on the 1st of july, then on the 1st of october, and so on. I'm having a hard time wrapping my head around how to achieve this. Any pointers?

1 like
8 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

Basically well formate for carbon date is YYYY-MM-DD .

        $nowMonth = date('F');
    $getInterval = 'get interval value from database';

    if($getInterval == 1){
        $doDate = Carbon::now()->addMonths(1);
        //do whatever you want
    }


    if($getInterval == 3){
        $firstJanuary = new \DateTime('first day of January 2016');
        $firstApril = new \DateTime('first day of April 2016');     
        ......
        //do whatever you want
        
    }

```

Hope will work for you... 
6 likes
RonB1985's avatar

Thanks, but I the january date etc was just an example. Ofcourse the start date could be the 5th of april for example or the 31st of december. Hardcoding it like you did in the above example is easy enough :)

Edit: Ok, using your example something popped into my head. And it's easy enough I believe. If the interval is 3, I just set up 4 variables, and for the first I will do the addMonths(3), for the second, addMonths(6), etc. Thanks again for getting me on the right track :)

3 likes
RonB1985's avatar

Yeah been looking at it, I always reference it a lot when working with dates and times. Thanks :)

3 likes
leafman's avatar

@ronb1985 was going through your thread, I wanted to ask if you were able to provide solution to your question. I have been battling with similar functionality for 3 days now, In my case, I have a field, start_date and end_date, and also duration. if the duration is 1, that means the post would be active for 24 hours, and if the duration is 3, the post would be active for 3 days, Now: I want to check the start date, and end date, if the duration is 1 which is (Post active for 24hours) send notification ones its 12 hours left and again, delete the post ones its expire, same also for the duration 3, after 24 hours send notification that the post is remaining 2 days, (I was thinking the best way to do this would be to update the start_date to current time, then calculate the intervals and send notification to user. Am using schedule for this operation to run every 1. Well, i have tried to add 24 hours to start date, then compare it with Carbon::now(), if its the same thing then send update db record and send notification, but am not getting the right output. I have also tried subtracting 24 hours to from the end time then compare it with current time to ensure the post dont exceed the end date, but not getting the right output. HINT: Any hint or guide to put me in the right direction towards solving this would be appreciated. Thanks. Further question cud be asked for clarity purpose. Thank you.

2 likes
leafman's avatar

@ronb1985 so I tried using this approach and i'm still testing it but i observed that

 //THis is the step that would process the boost reminder
        $boostPropertys = Property::where('property_boost_status', 1)
            ->where('property_boost_end_date', '>=', Carbon::now())
            ->get();
       // dd($boostPropertys);
        if ($boostPropertys){
            foreach($boostPropertys as $boostProperty) {
                //Get time from db and convert to carbon instance. 
                $startTime = (new Carbon)->parse($boostProperty->property_boost_start_date);
                $endTime = (new Carbon)->parse($boostProperty->property_boost_end_date);

                //Check if start date is equal to 24 hours with the current date. Note: My intention in the if statement is to retrieve every boost whose startTime == 1 day or 24 hours  

                if($startTime->addDays(1) == Carbon::now() and $endTime > Carbon::now()){
                    echo  $boostProperty->id ."\n";
                }
                else{
                    echo "No start time is == to current Time";
                }
            }
        }
        else{
            echo 'No Result for Boosted Property';
        }

while testing this, I observed that Carbon::now() return system time e.g 2019-04-24 16:24:00pm Now, my expectation in the above code is when startTime date: 2019-04-23 16:05:58 ->addDays(1) :

if  ( newStartDate (2019-04-24 16:05:58) == Carbon::now() (2019-04-24 16:24:00pm) ) {
//However This returns false, my guess is because of the seconds different
}else {
return false;
}

@bobbybouwmann please someone should put me in the right direction Thanks you

3 likes

Please or to participate in this conversation.