I’m using NOW() in sql query. I want to change it to another timezone. is that possible?
Tried this function
class common {
public function getTime() {
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Paris'));
return $date->format('Y-m-d H:i:s');
}
}
And getting following error
Catchable fatal error: Object of class common could not be converted to string in line below
$stmt = $this->db->prepare("INSERT INTO `items`
(`refno`, `color`, `size`, `qt`, `stackno`, `notes`, `price`, `add_date`)
VALUES (?, ?, ?, ?, ?, ?, ?, $this->common->getTime())") or die($db->error);
What did I do wrong?
The error is actually a PHP error, not an SQL error. Trying to evaluate complex variable expansion expressions inside string interpolation doesn’t work the way you’re doing it.
Try putting the object inside
{}braces:See the subheading Complex (curly) syntax in manual page http://php.net/manual/en/language.types.string.php
Re your comment:
You haven’t shown how you are setting
$this->common. Given the syntax you’ve shown, common needs to be an object instance of your classcommon. I’m guessing you’re trying to call the getTime() function without instantiating the class. If you want to use static methods of a class, you’ll have to do it this way:If you are unfamiliar with the difference between classes and objects, and the uses of
static, then you need to do some reading, e.g. Object-Oriented PHP.