will you run this script AFTER the deadline? And then you want previous weeks also? Or just one week at a time?
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!
$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
Please or to participate in this conversation.