A MySQL database has a variable "X" of type DateTime.
If text were to be entered into this variable as "2004-05-13 13:00:00" it would be accepted as a "DateTime" and the variable would not be overridden with the default (all 0s) right? Ok, simple enough.
Now, take the function I created to convert between timezones. You can view it below and basically get the jist of it, I don't expect you to really understand it as I made it and what I write rarely makes sense, is efficient, isn't already a built in function, etc.
Code: Select all
function est_to_pst_display() {
// get current date/time
$date = date("Y-m-d");
$time = date("H:i:s");
// explode
$newtime = explode(":",$time);
$newdate = explode("-",$date);
//get year
$year = date("Y");
//if we are going to go into negative numbers converting...
if($newtime[0] < 4) {
//get the bad number, add 24 then get rid of it
$less = $newtime[0];
$newtime[0] = $newtime[0] + 24 - $less;
//subtract a day, since we are still in yesterday
$newdate[2] -=1;
}
else {
$newtime[0] -= 3;
}
//if our day is now 0, subtract a month then assign the last day of last month to the day
if($newdate[2] == 0) {
$newdate[1] -=1;
//if we get 0, make it december and alter year data
if($newdate[1] == 0) {
$newdate[1] = 12;
$newdate[0] -=1;
$year -=1;
}
//make the date the last day of last month
$newdate[2] = date("t",mktime(0, 0, 0, $newdate[1], 1, $year));
}
//add a zero to the hour if needed
if($newtime[0] < 10 && strlen($newtime[0]) < 2)
$newtime[0] = "0" . $newtime[0];
//add a zero to the month if needed
if($newdate[1] < 10 && strlen($newdate[1]) < 2)
$newdate[1] = "0" . $newdate[1];
//add a zero to the day if needed
if($newdate[2] < 10 && strlen($newdate[2]) < 2)
$newdate[2] = "0" . $newdate[2];
//put variables back together
$new[1] = implode(":",$newtime);
$new[0] = implode("-",$newdate);
return $new[0] . ' ' . $new[1];
}It's the last line that confuses me. It looks like it would return a valid DateTime string, with the day as xxxx-xx-xx and time as xx:xx:xx. I have compared strings, echoed comparisons, and done literally everything I can think of but the bottom line is MYSQL DOES NOT ACCEPT THIS AS A DATETIME VARIABLE and thus defaults to the one with all 0s. You can understand how this is very bad. If you know WHY 2 strings that appear identical don't read the same to php/mysql please inform me.
Thanks