You have two solutions: use the PHP approach or the MySQL approach. (your question title is misleading, it's not a PHP NOW()
but a MySQL NOW()
).
With PHP, you can use the date_default_timezone_set
function for this.
It will set the default timezone for your PHP script.
Just remember to put it near the top so it applied to what you are doing.
BUT this will set the timezone for all PHP function, not your MySQL database NOW()
command.
To modify the timezone of MySQL, you have to use SET time_zone = timezonename;
. See this SO thread for setting MySQL timezone or the official documentation.
Example:
<?php
date_default_timezone_set('Asia/Calcutta');
$query = "INSERT INTO mytable (user_id, user_datetime) VALUES ".
"(".$_SESSION["user_id"].", ".date('Y-m-d H:i:s').");";
// OR
$query = "SET time_zone = 'Asia/Calcutta';"; // this will persist only for the current connection
$query .= "INSERT INTO mytable (user_id, user_datetime) VALUES ".
"(".$_SESSION["user_id"].", NOW());";