0

dI look for a date stored in the database. I save the result in a var but when I try to use that var later it gives me an error. I do not understand. How can I compare the present time with a time stored?

  1. I look for a date that is saved in MySql database. It is saved as hours and minutes. I echo it and it works:

    $result = mysqli_query($con, 'SELECT * FROM consulta  
                WHERE id=3  
                AND client=1');  
    while ($row = mysqli_fetch_array($result)) {  
    $begin = $row["inici"];  
    echo $begin;  
    }
    

    (this gives me: 15:30:00)

  2. I see what is the time, now. I echo it and it works:

    $time = date('H:i:s');   
    echo $time;
    

    (this gives me the present time in this format: 16:38:57 )

  3. I compare the two. It does not work. It gives me an error:
    Notice: Undefined variable: begin in /Applications/MAMP/htdocs/trobadaVirtual/begin.php on line 55

    if ($time = $begin) {  
    //do something  
    }
    
Nrc
  • 9,577
  • 17
  • 67
  • 114
  • 1
    You're probably missing an equal sign: `if ($time == $begin) {` – bobbel Feb 03 '14 at 15:17
  • I try it and it gives me the same error? – Nrc Feb 03 '14 at 15:20
  • undefined variable is a notice, not an error. Post the exact notice message you get, because you're not giving us much to go on here – Elias Van Ootegem Feb 03 '14 at 15:21
  • The warning is probably because you're not defining your `$time` or `$begin` variable. For further reading see: http://stackoverflow.com/a/4261200/3082272 – bobbel Feb 03 '14 at 15:32
  • Show your code in context, the error means that you have not set a value for `$begin` probably due to being wrapped in a function and not in the same scope as $time. – Schleis Feb 03 '14 at 15:33

1 Answers1

0

Create an instance of DateTime class and then, get the respective timestamp and compare with the timestamp of now.

Here is a sample code.

$dateTime = \DateTime::createFromFormat('H:i', $dateString);
$timestamp = $dateTime->getTimestamp();
if ($timestamp  === time()) {
  // do something important
} elseif ($timestamp  < time()) {
  // other important task
}
Ujjwal Ojha
  • 1,340
  • 10
  • 17