Note that there are some explanatory texts on larger screens.

plurals
  1. POTimestamp off in sql database for PHP and Javascript Chat Room
    primarykey
    data
    text
    <p>Basically I have been trying to follow a tutorial to create a chatroom.</p> <p><a href="http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/" rel="nofollow">Ajax Web Chat Tutorial</a></p> <p>I have it all done except the time is being displayed incorrectly and I can't figure out where to start. </p> <p>STEP 1. The chat displays the user's line right away (displayed correctly using javascript's Date() so the user thinks the system is speedy and quick)</p> <p>STEP 2. Then it goes to the database and stores the chatline where a column 'ts' stores the current_timestamp as a timestamp.</p> <p>STEP 3. After that Ajax grabs the chatlines from the database and refreshes the chat to display the 'real' chat lines it is displayed incorrectly even though the code is supposed to convert it to the user's timeszone. Here is the code to give you a sense of where I am at.</p> <p>Grab Time from chat database: // Returning the GMT (UTC) time of the chat creation:</p> <pre><code> $chat-&gt;time = array( 'hours' =&gt; gmdate('h',strtotime($chat-&gt;ts)), 'minutes' =&gt; gmdate('i',strtotime($chat-&gt;ts)) ); </code></pre> <p>This is then sent to javascript to update the chatroom's chat lines</p> <p>// All times are displayed in the user's timezone var d = new Date();</p> <pre><code> if(params.time) { // PHP returns the time in UTC (GMT). We use it to feed the date // object and later output it in the user's timezone. JavaScript // internally converts it for us. d.setUTCHours(params.time.hours,params.time.minutes); } params.time = (d.getHours() &lt; 10 ? '0' : '' ) + d.getHours()+':'+ (d.getMinutes() &lt; 10 ? '0':'') + d.getMinutes(); </code></pre> <p>EXAMPLE: User submits chatline..</p> <p>Javascript Prints 11:07... </p> <p>SQL saves current_timestamp as 8:07 ...</p> <p>PHP grabs from database and converts it to 12:07</p> <p>d.setUTCHours = 1348834072033</p> <p>and lastly params.time is printed using javascript to 8:07</p> <p>I don't think I'm missing anything else. Can anyone help? Really feel shitty trying to figure this out for a few days. Javascript is good... i think SQL is doing what its supposed to do and convert it to a universal time or something GMT? :P and PHP is close.. being an hour off but then javascript doesn't do what its supposed to do :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload