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

leafman's avatar

Compare two datetime with current date

I have a field, start_date and end_date, and also duration (int). 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 compare the start date, and end date, if the duration equalTo 1 (Post is active for 24hours)-> send notification when start_date hours is equalTo 12 , Then update date field to null when property has expired, same process for the duration 3 (3 days), send notification every 24 hours and update start_date to Carbon::now, repeat process every 24 hours until end date is equal to 12 hours send notification, lastly, update datefield to null when property expires. Below is my code Basically am trying to send notification of boosted property every 24 hours depending on the duration of the boost. eg 1, 3, 7, 14 days respectively. Am using laravel schedule.

    //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() returns e.g 2019-04-24 16:24:00pm. My expectation in the above code is when startTime date: 2019-04-23 16:05:58 ->addDays(1) :

//So example
if  ( $newStartDate eg.(2019-04-24 16:05:58) == Carbon::now() e.g(2019-04-24 16:24:00pm) ) {
//However Nothing happens, my guess is because of the seconds different
//I expected this to return true, so i can send notification and update startdate
}else {
return false;
}

Note: Am updating start_date to Carbon::now each time the system update start_date to carbon::now and send notification; so that i can fetch boosted properties first, before properties that are not boosted

$duration = // store the number of days. eg 1, 3, 7.. 
$start_date = // carbon::now()
$end_date = // future date eg 3 days time, 1 day time 7 days time etc
$boost_status = //1 means active boost // update to 0 when boost expires

//Code to Retrieve Property in view to display boostedProperties first, before properties that are not boosted,
//Please is this logic correct??
$myproperties = Property::where('properties.admin_status', 1)
            ->orderBy('property_boost_status', 'DESC')
            ->orderBy('property_boost_duration', 'DESC')
            ->orderBy('property_boost_start_date', 'DESC')
            ->orderBy('created_at', 'DESC')
            ->get();
0 likes
8 replies
bobbybouwmann's avatar

You're comparing two dates here that are never going to be the same

'2019-04-24 16:05:58' == '2019-04-24 16:24:00'

This is never going to equal. Instead you need to compare in a different way.

You can compare two dates like so

$newStartDate = Carbon::now();
if (Carbon::now()->isSameDay($newStartDate)) {
    // Do your thing
}

Documentation: https://carbon.nesbot.com/docs/#api-comparison

leafman's avatar

@bobbybouwmann i havent tested this yet, but my guess would be to use this since i want to add 24 hours to startDate and store in newStartDate-- when is true with current time perform operation:

  1. newstartDate = startDate->addDays(1)
if(Carbon::now()->isSameDay($newStartDate) {
//Meaning: 24 hours has been added to start date
//Perform operation here 
}
 or 
(Carbon::now()-->equalTo($newstartDate )){
//Meaning: 24 hours has been added to start date
//Perform operation here 
}
bobbybouwmann's avatar

@leafman equalTo only works if they dates and times are exactly the same! If there is one minute difference it won't work. It depends on what you want to check or if it's in a cronjob that runs every minute that equalTo is fine, but if that is not the case it will almost never result in true.

leafman's avatar

any better implementation instead of using the equalTo I wud prefer calling the cronjob every hour

$startDate = new Carbon()->parse($model->startDate)->addDays(1)
 $newDate = $startDate;
//Please would cabon::now()->isSameDay($newDate) return the same value as $newDate ?
//E.g
//startDate = 28th/04/2019 T 9:55 pm
//newDate = 29th/04/2019 T 9:55 pm
Question
Would Carbon::now()->isSameDay($newStartDate) return true?
//when futureDate and time  is 29th/04/2019 T 9:55 pm ..?
neeonline's avatar

Why not use diffInHours?

$now = \Carbon\Carbon::now();
$endDate = \Carbon\Carbon::parse($boostProperty->property_boost_end_date);

$remainingBoostHours = $now->diffInHours($endDate);

Which this idea you can schedule a cronjob every hour and then handle it if remaining is equal 48, 32, 24, 12, 1, etc...

leafman's avatar
leafman
OP
Best Answer
Level 1

@neeonline Pls, help me crosscheck this, my intention is that every 24 hours the system shud send a notification to the users based on their active boost. The duration of boost cud range from 1 day to 14 days. so now am checking the difference between currentTime to BoostStartDate if result == 24 hours. system shud update BoostStartDate to latest time. Intention: to make it easy to fetch active boostedProperty record first and to send Notification to the user of the remaining time. Please read my question and see if this implementation would provide a solution to it.

$boostStartDate = (new Carbon)->parse($boostProperty->property_boost_start_date);
                $boostEndDate = (new Carbon)->parse($boostProperty->property_boost_end_date);
                //Check Differences in Hours
                $curentTime = Carbon::now();
                $diffInStartDate = $curentTime->diffInHours($boostStartDate); //24 means 1 day to d future
               
                $diffInEndDate = $boostEndDate->diffInHours($curentTime); //72
                //echo $diffInStartDate . '..';
                
                if($diffInStartDate == 24 && $diffInEndDate > 12) {
                    //Boost active for 24 hours; Update startDate to currentTime and sendNotification
                }
                if($diffInEndDate == 12) {
                    //if The difference is == 12 that means the boost is less than a day and it would expire in 12 hours
                    
                }
                if($diffInEndDate <= 0) {
                    //That means the boost has expired.
                    
                }
bobbybouwmann's avatar

This looks pretty good. I can't really decide if you have the correct business logic here, but codewise everything looks correct ;)

1 like

Please or to participate in this conversation.