Using php Timestamps with Timezone

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I have numerous databases, all of which use timestamp format date/times, which are easy to echo according to user timezone preferences using putenv ("TZ=".$_SESSION['MM_UTZ']); and echo date($_SESSION['MM_UTF'], ($whatever));

The only problem is, I have only used these for stamping "date created" or "date edited" values using Time()

What I am trying to achieve is:

User 1 in UK enters date and time, using a javascript datetime picker (which normally enters data into field in Y-m-d H:i:s) and the php inserts this in timestamp format according to timezone.

User 2 in US looks at date and time and this is echo'd according to his timezone.

Sounds simple...

I have managed to get so far with the new DateTime object which incorporates the user timezone preference ($utz)

$ndttime= new DateTime($timefield, new DateTimeZone($utz));

I can then echo this effectively using

echo $ndttime->format('$utf'); ($utf being user timeformat preference)

My stupid problem is trying to get the $ndtime into the database!!!!

I know this sounds ridiculous but I use the DW insert wizard which only uses values from a form and if I echo $ndtime into a hidden field, it doesn't process this value in time.

Below is the current insert script.

Is this simple?


Heeeeelp.



Attached Code
<?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}}$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO EVENTS (EVSTART) VALUES (%s)", GetSQLValueString($_POST['evstart'], "text")); mysql_select_db($database_freecrm, $freecrm); $Result1 = mysql_query($insertSQL, $freecrm) or die(mysql_error()); $insertGoTo = "../actionconfirmed.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));}?>
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
what i've been doing for my site is use the time() function (submits in UTC) for any times that are being saved into the database and then alter the timestamp dynamically by their timezone when the script runs. if it's GMT (UTC+0000) then it doesn't get altered. if it's EST (UTC-5000) then I would do 3600*-5 = -18000 seconds (equivalent to 5 hours behind UTC).
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
what i've been doing for my site is use the time() function (submits in UTC) for any times that are being saved into the database and then alter the timestamp dynamically by their timezone when the script runs. if it's GMT (UTC+0000) then it doesn't get altered. if it's EST (UTC-5000) then I would do 3600*-5 = -18000 seconds (equivalent to 5 hours behind UTC).

Yeah - I understand that and use the putenv function to bypass this need.

But...

When a user physically enters a date and time, they usually get the format wrong... so a freely available JS date/time picker resolves that; returning the format recognised by MySQL - i.e. Y-m-d H:i:s.

My problem is that because its JS and client side, it picks up the local time.

What I'm trying to do when entering the date into the database from the form field, it needs to also capture the locale timezone as well so that when another user from somewhere else looks at the echo'd date, it is automatically amended to show in their local time.

I have the funtion sorted but all I need to do is get it into the database with the script in the right place!
 
Last edited:
Top