0

I loaded my php script on hosting provider "byethost.com". I am trying to set the time of the records in the created_at to the local time of the client but I am always getting the GTM time. How can I get it to work?

 <?php
 $data = json_decode ( file_get_contents ( 'php://input', true ) );

$mac = $data->{'mac'};
$latitude = $data->{'latitude'};
$longitude = $data->{'longitude'};
$route =   $data->{'route'};

$timeZone = "Europe/Berlin";
$print = date_default_timezone_set($timeZone);

$con = new mysqli ( "domin.com", "username", "password", "database" );


// check whether route's table exist.
$results = $con->query ( "SHOW TABLES like 'bus' " ) or die ( mysqli_error () );

if (($results->num_rows) == 1) {$sql = "REPLACE INTO bus(mac, route, latitude, longitude, created_at)
          VALUES( ?, ?, ? , ?, ? )";
  $stmt = $con->prepare($sql);

  if(false === $stmt){
    echo "prepare()  failed: ";
  }

  $rc = $stmt->bind_param("sssss",$mac,$route, $latitude,$longitude, $print);
 echo $rc;
  if ( false===$rc ) {
  echo "bind_param() failed: ";
}

  $rc = $stmt->execute();

  if ( false===$rc ) {
 echo "execute failed.";
  }

  $stmt->close();

} else {
  $create =  "CREATE TABLE bus
       (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        mac VARCHAR(30) NOT NULL UNIQUE,
        route int(11) ,
     latitude FLOAT(10,6) NOT NULL , 
     longitude FLOAT(10,6) NOT NULL,
     created_at TIMESTAMP NOT NULL" ;
   $stmt = $con->prepare($create) or die ( $con->error );
  $stmt->execute();

  $stmt->close();

}
The Time
  • 697
  • 5
  • 12
  • 26
  • 1
    You probably want to store all time in the DB in GMT and display time in the user's time zone. See the answer from @fijiaaron in this SO question http://stackoverflow.com/questions/3792066/convert-utc-dates-to-local-time-in-php. – steve klein Apr 30 '15 at 10:32
  • @steve: No I want to store the data (for testing purpose ) in the bus table in client timezone. – The Time Apr 30 '15 at 10:47

1 Answers1

1

Maybe try with this?

date_default_timezone_set($timeZone);

Time zone can be something like:

"Europe/Oslo"

For the complete list of supported time zones see http://php.net/manual/en/timezones.php

Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27
  • I did it as you said and I am getting the error `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7 ` This one is line 7. `created_at TIMESTAMP NOT NULL`? I updated my code please take a look. – The Time Apr 30 '15 at 14:03
  • To be honest I do not see how can date_default_timezone_set() cause a SQL error? O.o Can you try putting this on top of your code (lines 6 and 7 to lines 2 and 3) and see if it still says line 7? – Slobodan Antonijević May 25 '15 at 15:22