1

I have an issue while comparing two date inside if condition. I am providing my code below.

$erresult = mysqli_fetch_array($qrylast);
$ticket = $erresult['ticket_id'];
if ((date("Y-m-d") == $erresult['date'])) {
    $id = sprintf("%03d", $ticket++);
    $fields = array("date", "ticket_id ");
    $tablename = "db_ticket";
    $values = array(date("Y-m-d"), $id);
    $id1 = db_insert($tablename, $values, $fields);
    if ($id1) {
        $ticket_id = 'W1' . date("Ymd") . $id;
    }
} else {
    $id = '001';
    $fields = array("date", "ticket_id ");
    $tablename = "db_ticket";
    $values = array(date("Y-m-d"), $id);
    $id1 = db_insert($tablename, $values, $fields);
    if ($id1) {
        $ticket_id = 'W1' . date("Ymd") . $id;
    }
}

Here I need to compare today's date with date save inside database. My saved date inside database datatype is also date but here always else part is executing. In my code I have one condition (date("Y-m-d")==$erresult['date']) and this condition is never executing even two date are same.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130

2 Answers2

2

Try

if(strtotime(date("Y-m-d")) == strtotime($erresult['date']))

follow :- How to compare two dates in php

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
2

You can use the strtotime() function to compare the two dates e.g

if(strtotime(date("Y-m-d")) == strtotime(date("Y-m-d",$erresult['date']))) 
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
Dilip Kale
  • 71
  • 7