Asked  6 Months ago    Answers:  5   Viewed   37 times

How can I compare two dates in PHP?

The date is stored in the database in the following format


If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?

I tried this,

$today = date("Y-m-d");
$expire = $row->expireDate //from db

if($today < $expireDate) { //do something; }

but it doesn't really work that way. What's another way of doing it?



in the database the date looks like this 2011-10-2

Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.

Tuesday, June 1, 2021
answered 6 Months ago

To do a comparison like this you need to do separate comparisons. If $d is the date you want to compare, $d1 is the earlier date, and $d2 is the later date, it would be something like:

if ((strtotime($d) > strtotime($d1)) and (strtotime($d) < strtotime($d2))) {
    return true;
} else {
    return false;
Wednesday, March 31, 2021
answered 9 Months ago

Condition is wrong, it should have two equals to signs ==

if($row['posted'] == date('j-n-Y'))

Please Note that one equal to sign is for assignment, it means that

$row['posted'] = date('j-n-Y')

The above code will assign the value of date('j-n-Y') into $row['posted']

And your assignment will do nothing but just return true, so every time it will go into the if condition block

where as the two equals to signs == are use for compression. so in your condition with double equals to, the code will only go into the if condition block when your $row['posted'] is equal to date('j-n-Y')

NOTE: mysql_* functions are Officially deprecated (as of PHP 5.5. It's likely to be removed in the next major release.)

you need to have a look at this Why shouldn't I use mysql_* functions in PHP?

Saturday, May 29, 2021
answered 7 Months ago

You will have to make sure that your dates are valid date objects.

Try this:

$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));

You can then perform the strtotime() method to get the difference.

Tuesday, June 1, 2021
answered 6 Months ago

You could try

indx <- colSums(A!=B) 
which(!!indx) #gets the index of different columns
# Col2 Col3 
# 2    3 
which(!indx) #gets the index of similar columns
#Col1 Col4 Col5 
# 1    4    5 
length(which(!indx) )
#[1] 3


A <- data.frame(Col1= c(7,2), Col2= c(9,4), Col3= c(0,5), 
      Col4= c(0,3), Col5=c(2,3))
B <- data.frame(Col1= c(7,2), Col2= c(8,4), Col3= c(6,5), 
     Col4= c(0,3), Col5=c(2,3))
Sunday, August 29, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :