Daniël Cronk Daniël Cronk - 4 months ago 21
PHP Question

echo issues with datetime-local format

I've tried numerous times to figure this out and I'm failing to update the code on an older page I've taken control of.

I've got some code (yes, I know I'm still working with mysql, but it's an old page and I haven't the time to rewrite everything for mysqli at the moment).

Code:

<?php
mysql_connect("some hostname", "someusername", "somepassword") or
die(mysql_error());
mysql_select_db("somedatabase") or die(mysql_error());

$id = (int)$_GET['id'];
mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
$order = " SELECT *
FROM tbl_patientdoseinformation
WHERE patientdoseID = '$id'";

$result = mysql_query($order);
$datetimestated = date('Y-m-d\TH:i:s', strtotime($row['datetimestated']));

$row = mysql_fetch_array($result);
?>


and an form with input code of:

<input type="datetime-local" name="stated" value="<?php echo $datetimestated; ?>">


However, when it echos out, it is most certainly not the date/time that is in the database.

The value of the record I"m working with should be
07/20/2016 10:00 AM

The value that echos is:
12/31/1969 06:00 PM

Answer

You are attempting to use $row before you retrieve it from the result set

<?php   
    mysql_connect("some hostname", "someusername", "somepassword") or   
 die(mysql_error()); 
    mysql_select_db("somedatabase") or die(mysql_error()); 

    $id = (int)$_GET['id'];
    mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
    mysql_query("SET NAMES 'utf8';");//POST
    $order = "  SELECT *
                FROM tbl_patientdoseinformation
                WHERE patientdoseID = '$id'";

      $result = mysql_query($order);

      $row = mysql_fetch_array($result);

      // move to after the fetch
      $datetimestated = date('Y-m-d\TH:i:s', strtotime($row['datetimestated']));

?>