3

I want to compare two dates and time values in PHP. One date is coming from MySQL, and second one is the current date. I want to run some code when both dates are the same. I tried the code below, but condition satisfies any time which is wrong.

$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
    //I want to run some code here
}else{
}

What is wrong with the code? I also tried to covert both dates with strtotime() before comparing, but it gave me the same issue. The above condition satisfies any time even if both dates are different.

5
  • How precisely do you want to compare them? Up to hours? Commented Oct 17, 2016 at 5:57
  • No, its not duplicate question, I read that question but did not get answer what I want, that is why I asked new one. Commented Oct 17, 2016 at 5:57
  • I want to compare date and time(hours and minutes). I don't want to compare seconds. Commented Oct 17, 2016 at 5:58
  • Also you compare like that: $var = $row['send_date']; if((time()-(60*60*24)) == strtotime($var)){ ... } Commented Oct 17, 2016 at 5:59
  • What is your output of $row['send_date'] Commented Oct 17, 2016 at 6:14

3 Answers 3

9

Try this :

$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if(strtotime($current_datetime) == strtotime($send_date)){
    //I want to run some code here
}else{
}

Hope it helps !!!!

Sign up to request clarification or add additional context in comments.

4 Comments

What you have changed?. Explain properly.
@AHJeebon : i did convert the dates in to strtotime format because that's the right way to compare the date values.
@KaushaThakkar : Yes, this code runs fine for me. Thanks
@KetanLathiya Thanks !! can you please give it a green tick :)
2

One way is to fetch the Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) from MySQL, then operate on the numbers:

$row = get_db_row("SELECT UNIX_TIMESTAMP(send_date) AS send_date_ts
  FROM table WHERE $condition");
$hours = (int) ($row['send_date_ts'] / 3600);
$current_hours = (int) (time() / 3600);
if ($hours == $current_hours) {
  // current hour
}

Timestamps are convenient because:

  • there is no need to take the format into account;
  • operations on numbers are usually faster;
  • the code looks cleaner.

Comments

-1

Try this. On my server is working just great I've got something else because they aren't equal. Date which I receive from database is type datetime format 2015-04-13 09:03:49

<?php
     $current_datetime = strtotime(date('Y-m-d H:i'));
     $send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
     if($current_datetime == $send_date){
         //I want to run some code here
         echo 'something';
     }else{ 
         echo 'something else';
     }

Output:

echo $current_datetime . '<br/>';
2016-10-17 09:19

echo $send_date .'<br/>';    
2015-04-13 09:03

// result
something else

1 Comment

You are comparing a string to an integer, can't work. strtotime(date('Y-m-d H:i')) is never equal to date("Y-m-d H:i", strtotime($row['send_date'])).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.