I am trying to check if one datetime is after another, but it doesn't seem to be working. I am simply using the >=
operator, but it is not working as expected?
Here is the code:
public function hasCompleted() {
return Carbon::parse($this->withdrawable_at) >= Carbon::now();
}
I am using the Carbon package, a DateTime PHP library found here
It seems to return true for the following data, which it shouldn't.
2020-11-13 02:59:04, 2020-11-12 15:17:31
My Answer:
strtotime() function to convert the dates to timestamps and then comparing them using the greater than operator. Here is an example code snippet:
php
$date1 = '2022-01-01 12:00:00';
$date2 = '2022-01-02 12:00:00';
if (strtotime($date1) > strtotime($date2)) {
echo "Date 1 is after Date 2";
} else {
echo "Date 1 is not after Date 2";
}
Make sure that the date format is correct and that the dates are in the correct order when comparing them. If the comparison is still not working, you may need to check for any timezone differences or other factors that could affect the comparison.
Rate this post
5 of 5 based on 5564 votesComments