I have a hidden input where its supposed to send the current date to its respective place in the database:
<input type="hidden" name="DATE" value="<?php echo date("Y/m/d"); ?> />
As we all know, the date() function returns the server date and not the user's, so I changed the timezone:
<?php date_default_timezone_set('America/Mexico_City');
$MexDate = date("Y/m/d"); ?>
Then, I changed the value of the hidden input, so know I could send the current date according to Mexico's timezone.
<input type="hidden" name="DATE" value="<?php echo $MexDate; ?>" />
The problem here is that insted of writing the date, it writes nothing (0000-00-00). That didn't happen when I was using just date()
without changing the timezone. What happened here?
Maybe because you haven't specified the right format. I'd suggest you specify the format as Y-m-d instead of Y/m/d
$MexDate = date("Y-m-d");
Also, another way would be using mysql NOW() function. It will insert the current date in the database.
Once you change the TimeZone through through php. if you check this link http://dev.mysql.com/doc/refman/5.5/en//time-zone-support.html it says
Per-connection time zones. Each client that connects has its own time zone setting, given by the session time_zone variable. Initially, the session variable takes its value from the global time_zone variable, but the client can change its own time zone with this statement:
Therefore you need to set the same timezone in mysql also and that can be done through this command. Suppose $db is your mysql object then use
$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);
$db->query("SET time_zone = '$offset'");
Got this from https://www.sitepoint.com/synchronize-php-mysql-timezone-configuration/