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

stevelaracasts's avatar

Creating a deadline date with Carbon and get records upto the deadline.

I'm trying to create a deadline date query and need a little help with the date and time.

So I have a deadline day and time of Thursday at 5pm (its a variable but for the sake of this example).

I want to query the db for all records from last Thursday at 5:01pm until the deadline of next Thursday at 5 pm, so the previous seven days from the deadline.

I want to run this query until the deadline is reached then start querying record for the next deadline period ie Thursday 5:01pm to next Thurday 5:pm

So I have the following code which works

 $dayOfWeek = 'Thursday';
 $time = '17:00:00';
 $fromdate = Carbon::now()->next($dayOfWeek)->subDays(7)->setTimeFromTimeString($time)->format('Y-m-d H:i:s');
 $enddate = Carbon::now()->next($dayOfWeek)->setTimeFromTimeString($time)->format('Y-m-d H:i:s');

But the problem I am having it the code doesn't seem to respect the time portion. Once I get to Thursday it starts returning results for the next time period, no matter the current time!

So my question is how can I add/amend to my query so it keeps running the "last 7 day period" until the deadline day/time then start querying the next period?

thanks!

0 likes
7 replies
Snapey's avatar

will you run this script AFTER the deadline? And then you want previous weeks also? Or just one week at a time?

stevelaracasts's avatar

@Snapey thanks for replying!

Just one week at a time. I've been trying to figure this out for a few days now so I losing myself and might not have explained it clearly!?

If I give this as a real life example and this might help!? Let say I'm a sales manager and the sales target dealine is Thursday at 5pm, every day upto Thursday 5pm I want to see if we are near our target so I need to query all the sale from last Thursday at 5:01pm upto the coming Thursday 5pm, so 1 week. Once we are past Thursday 5pm I need to start querying the db for next weeks deadline target, so the current Thursday at 5:01pm to next Thursday 5pm.

forkingbeardman's avatar

Can you try Carbon::parse("this $dayOfWeek")->setTimeFromTimeString($time) compare it to now and if now() is less, then the above is your $endDate else exactly what you had? Once you have $endDate, $fromDate is always -7 (if i understood the problem correctly). Problem is next always returns next occurence and if today is thursday it will return the date of next thursday and not today. P.S Sorry for poor formatting i am currently on my phone

newbie360's avatar

@stevelaracasts First my english not very good, if i understand correctly, you need query between a time range?, something like this?

    $a = now()->previous('Thursday')->setTimeFromTimeString('17:00:00')->addSecond();

    $b = now()->next('Thursday')->setTimeFromTimeString('17:00:00');

    $c = Carbon::parse('2024-11-02 18:34:56')
        ->previous('Thursday')
        ->setTimeFromTimeString('17:00:00')
        ->addSecond();

    $d = Carbon::parse('2024-11-02 18:34:56')
        ->next('Thursday')
        ->setTimeFromTimeString('17:00:00');

    dd(
        $a->format('Y-m-d H:i:s'),
        $b->format('Y-m-d H:i:s'),
        'Time Remaining: ' . now()->longAbsoluteDiffForHumans($b, 4),
        $c->format('Y-m-d H:i:s'),
        $d->format('Y-m-d H:i:s'),
    );

result

"2024-10-24 17:00:01"

"2024-10-31 17:00:00"

"Time Remaining: 6 days 4 hours 10 minutes 25 seconds"

"2024-10-31 17:00:01"

"2024-11-07 17:00:00"
forkingbeardman's avatar
Level 36
$dayOfWeek = 'friday';
$time = '11:00:00';

$compareDate = Carbon::parse("this $dayOfWeek")->setTimeFromTimeString($time); //returns today or next occurence of the day
$endDate = Carbon::now()->greaterThan($compareDate) ? Carbon::now()->next($dayOfWeek)->setTimeFromTimeString($time) : $compareDate; //compare current time

$fromDate = $endDate->clone()->subDays(7); //set fromDate		
1 like
stevelaracasts's avatar

@forkingbeardman well you did it a hell of a lot tidier than me but I think I got to the same solution!

 $dayOfWeek = 'Thursday';
 $time = '17:00:00';

   $currentDateTime = Carbon::now(); // Get the current date and time
    $targetDateTime = Carbon::now()->next($dayOfWeek)->setTimeFromTimeString($time); // Set the target day and time

    // Check if current time is past the target time
    if ($currentDateTime->greaterThan($targetDateTime)) {
        echo "We are past " . $targetDateTime;
        $fromdate = Carbon::now()->previous($dayOfWeek)->setTimeFromTimeString($time)->addMinutes(1)->format('Y-m-d h:i:s');
        $enddate = Carbon::now()->previous($dayOfWeek)->setTimeFromTimeString($time)->addDays(7)->format('Y-m-d h:i:s');
    } else {
        echo "We are before " . $targetDateTime;
        $fromdate = Carbon::now()->previous($dayOfWeek)->setTimeFromTimeString($time)->addMinutes(1)->format('Y-m-d h:i:s');
        $enddate = Carbon::now()->previous($dayOfWeek)->setTimeFromTimeString($time)->addDays(7)->format('Y-m-d h:i:s');
    }

1 like
forkingbeardman's avatar

Hi there! I’m really happy if I could help! 😊 You’re definitely on the right track, but I have a question about the implementation. I might be misinterpreting something from the Carbon documentation, so please feel free to correct me if I’m off here.

If I understand it correctly, $currentDateTime might never be greater than $targetDateTime due to how Carbon handles the next() method. Let me use today’s date as an example. If $dayOfWeek = 'Friday';, and today is Friday (October 25, 2024), calling Carbon::now()->next($dayOfWeek); would still return next Friday, which is '2024/11/01'. So, $targetDateTime will always be in the future, no?

Hope this helps clarify things a bit!

Please or to participate in this conversation.