I have this code where I connect to database and the code is working properly. However the now() function of MySQL is out of sync with PHP set default time zone function. There is 8 hour difference between the now() in mysql and the time zoned $datenow= new DateTime() in php. I am comparing the PHP date with MySQL now. Therefore, I wanted to set the database time zone as well.
private $host="9999999";
private $user="99999999";
private $pwd="8888888";
private $dbName="88888888";
protected function connect(){
$dsn='mysql:host='.$this->host.';dbname='.$this->dbName;
$pdo = new PDO($dsn, $this->user, $this->pwd);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
I wanted to set the database time upon connection. I have this code I wanted to adapt it to mine. This is the code below. I came across this code and I thought it could be of help but I am finding it hard to implement with my already working code.
CODE TO ADAPT
$now = new DateTime();
$mins = $now->getOffset() / 60;
$sgn = ($mins < 0 ? -1 : 1);
$mins = abs($mins);
$hrs = floor($mins / 60);
$mins -= $hrs * 60;
$offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
//Your DB Connection - sample
$pdo = new PDO('mysql:host=localhost;dbname=test', 'dbuser', 'dbpassword');
$pdo->exec("SET time_zone='$offset';");
Please, how do I implement the two codes together
Link to the code I am adapting is this.
FULL CODE 1.(index.php)
$datedefault=date_default_timezone_set($zone['continent'].'/'.$zone['city']);
2.(dbh.class.php)
class Dbh{
private $host="22222";
private $user="777777";
private $pwd="6666";
private $dbName="55555";
protected function connect(){
$now = new DateTime();
$mins = $now->getOffset() / 60;
$sgn = ($mins < 0 ? -1 : 1);
$mins = abs($mins);
$hrs = floor($mins / 60);
$mins -= $hrs * 60;
$offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
$dsn='mysql:host='.$this->host.';dbname='.$this->dbName;
$pdo = new PDO($dsn, $this->user, $this->pwd);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->exec("SET time_zone='$offset';");
return $pdo;
}
}
This is where I am using the class 3. (user.class.php)
class Users extends Dbh{
protected function getByandbye($email, $phone){
$sql="SELECT * FROM user WHERE `email`=? OR CONCAT (`pre`,`phone`)=?";
$stmt= $this->connect()->prepare($sql);
$stmt->execute([$email, $phone]);
$user=$stmt->fetch();
return $user;
}
}