skip to Main Content

I’m building a tool that will be confusing to the user if it’s on server time due to timezones. I want to pass the local system time to PHP so DATETIME objects passed into the DB are based on their local time and date.

I found this: Convert JavaScript new Date() to php DateTime() which explains that I have to do weird stuff to the JS date function to make it compatible (using jquery to throw the time into the form just before submit):

$('input[name=now]').val('@' + Math.round(new Date().getTime()/1000));

On the PHP side, this is what I’m doing:

$local_time = (int)$_REQUEST['now'];
$today = new DateTime($local_time);

Simply, I’m trying to set the Datetime value to the passed JS value. I’ve been at this for an hour trying every page and guide I can find, but nothing is working. I keep getting various errors and, when it actually seemed to work, it was actually ignoring the $local_time value and just picked the server time instead.

With the code the way it is, I get the following error

Fatal error</b>:  Uncaught Exception: Failed to parse time string (0) at position 0 (0):
 Unexpected character in {pathtofile}.php:1394

NOTE: I have tried passing various JS formats (toGMTString, toISOString) and it either throws an error or ignores the value and uses server time instead.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, I don't know what I was doing wrong before - probably the wrong combination of functions, but here's what worked:

    // Outputs datetime in this format:  "8/19/1975, 4:15:30 PM"
    $('input[name=now]').val(new Date().toLocaleString());
    

    In php, my mistake was trying to send that to strtotime and then use that as an input to DateTime when DateTime can use the string directly as-is:

    $local_time = new DateTime($_REQUEST['now']);
    

    From there I"m able to make many different and useful calculations like so:

    if ('today' == $when) $when = $local_time;
    else if ('tomorrow' == $when) $when = $local_time->modify('+1 day');
    else if ('this_week' == $when) $when = $local_time->setTimestamp(strtotime('next saturday 11:59pm',$local_time->getTimestamp())); // The next saturday from today. 
    else if ('this_month' == $when) $when = $local_time->setTimestamp(strtotime('last day of this month 11:59pm',$local_time->getTimestamp()));
    

    And that solved the problem.


  2. You are looking for PHP’s strtotime().

    If you want to store the date as a string on the server side, Date.toISOString() is your best option.

    Note that Unix timestamps are time zone-independent and you will not get different results unless the user has their system time set incorrectly. If you save a Unix timestamp using an incorrect time as the base and display it back, they will see things as they submitted them. It doesn’t matter how the server stores them.

    When it comes time to work with them on the server, strtotime() will automatically parse them, and most formats, into the correct Unix time. If your only concern is the user experience, I recommend using Moment.js for managing time zone problems on the client side, and let the server use the actually correct Unix time.

    What you are suggesting could cause other problems if others can see the saved time stamps. e.g. If user Alice has their clock set incorrectly on their PC and makes a new post, and you use her system time as the time the post was made, you may end up with weird situations like posts appearing out of the order they were made. Alice’s comment may appear to be posted before the comment she was responding to.

    When in doubt, always use the correct, real-world time on your server, then display the times based on the system of the client.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search