I run a website for a local organisation where members log in using their own logins. I have quite basic knowledge, so this may be a simple fix..
I have the following code as part of the php run when they successfully log in which saves their log in date & time to the MySQL database.
$stmt = $pdo->prepare('UPDATE accounts SET lastlogin = CURRENT_TIMESTAMP WHERE id = ?');
The problem is that the date and time stored is 2 hours behind our local time (New Zealand). I've searched around the net for this answer but get lost in everything that's out there! What am I missing in this code to save the date & time as local?
Thanks!
CodePudding user response:
That's because the database's time zone differs from your local time zone.
Make sure you first set default time zone in PHP like so:
date_default_timezone_set('Pacific/Auckland');
This is how I fixed it.
$current_time = date('Y-m-d H:i:s');
$stmt = $pdo->prepare("UPDATE accounts SET lastlogin = '{$current_time}' WHERE id = ?");
