0

I'm trying to compare the date pass from previous page with the date (unix timestamp format) stored in database. My code as below. Error : Recoverable fatal error: Object of class DateTime could not be converted to string in C:\xampp\htdocs\check.php on line 14

How should I modify it to be able to compare with the "datetime2" and store as the 10 digit unixtime.

Basically I do not want to convert it to normal date. just store as it is, 10 digit unixtimestamp.

Please help to look into the foreach loop as well. I want to check if the selected date has appeared in database.

Thank you

<?PHP
 session_start();
 
 $userid = $_SESSION['userid'];
 $datetime = $_POST['datetime'];
 $available = 0;
  
 $date = $datetime; $datetime2 = new DateTime($date); echo $datetime2->getTimestamp();



 $con = mysqli_connect('localhost', 'root', 'abc123');

 $query = "SELECT COUNT * FROM reservation WHERE InstallDate='" . $datetime2 . "'";

     if (!$con)
       {
       die('Could not connect: ' . mysqli_error($con));
      }


     mysqli_select_db($con, 'btr');


     foreach(mysqli_query($con, $query )as $row)
 {
  if($row <=1)
  {
   $available = 1;
   echo $available;
  } 
  
 }


     mysqli_close($con);

?>
zach89
  • 11
  • 2
  • Why don't you use `getTimestamp()` in order to save value? – guyaloni Nov 14 '17 at 18:15
  • I don't get it. – zach89 Nov 14 '17 at 18:19
  • You don't want to convert it to a date, you just want to use the 10 digit string. However, what you are doing is starting with a 10 digit string, creating a DateTime object from that and then passing that DateTime object to your SQL query. – miknik Nov 14 '17 at 18:20
  • how should i modify this : $query = "SELECT COUNT * FROM reservation WHERE InstallDate= '$datetime2' "; – zach89 Nov 14 '17 at 18:27
  • Possible duplicate of [Convert from MySQL datetime to another format with PHP](https://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php) – FredTheWebGuy Nov 14 '17 at 18:28
  • getTimestamp gave me 10 digit of time. that is what i going to use o store and compare. the problem is with the sql now. m i right ? – zach89 Nov 14 '17 at 18:31
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Nov 14 '17 at 18:42
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use manual escaping and string interpolation or concatenation to accomplish this because you will create severe [SQL injection bugs](http://bobby-tables.com/). Accidentally unescaped data is a serious risk. Using bound parameters is less verbose and easier to review to check you’re doing it properly. – tadman Nov 14 '17 at 18:42

1 Answers1

0

Instead of using object of class DateTime, try convert it to string in a propriate way as follows:

$query =
"SELECT COUNT * FROM reservation WHERE InstallDate='"
.$datetime2->format('d/m/Y') 
."'";

Also adjust d/m/Y separators for your database, e.g.MySQL uses d.m.Y separation.

Ignas
  • 389
  • 2
  • 13