+ Reply to Thread
Results 1 to 6 of 6

Thread: Using php Timestamps with Timezone

  1. #1
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Using php Timestamps with Timezone

    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));}?>

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Using php Timestamps with Timezone

    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).

  3. #3
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Using php Timestamps with Timezone

    Quote Originally Posted by xPlozion View Post
    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 by freecrm; 08-28-2008 at 12:21 PM.

  4. #4
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: Using php Timestamps with Timezone

    I would help debug your code, but it's all thrown together in one line. Instead: http://forums.x10hosting.com/438716-post4.html
    - When in doubt, refer to the PHP manual.

  5. #5
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Using php Timestamps with Timezone

    Thanks Scopey - I have continued this on the following forum (as suggested above) as it might be of use to both of us.

    http://forums.x10hosting.com/program...e-support.html

  6. #6
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Using php Timestamps with Timezone

    Please close this thread

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. Process PHP from a MySQL array
    By tnl2k7 in forum Programming Help
    Replies: 12
    Last Post: 05-05-2008, 10:02 AM
  4. PHP Easter Eggs
    By dragoneye_xp in forum Off Topic
    Replies: 3
    Last Post: 06-14-2006, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers