Jared
Jared

Reputation: 113

javascript displaying a different date than php

I have a php script that generates a date object from a database value like this:

$dt = new DateTime($string_from_database);   // In YYYY-mm-dd format
$jsonValue = $dt->format('U');

This is retrieved by my JS using AJAX. I feed it into a jQuery table like this:

            //DateStart
        {
            'sName': 'date_start',
            'iDataSort': 2,
            'bSearchable': false,
            'fnRender': function(obj) {
                var dStart = new Date(parseInt(obj.aData['DateStartJson']) * 1000);
                var dEnd = new Date(parseInt(obj.aData['DateEndJson']) * 1000);
                if (obj.aData['DateStartJson'] == obj.aData['DateEndJson'])
                    return dStart.toDateString().substr(4);
                else
                    return dStart.toDateString().substr(4) + ' -<br/>' + dEnd.toDateString().substr(4);
            }
        },

Some users are reporting that the JS time displayed is a day earlier than the date as displayed via PHP directly.

PHP displays: Aug 24, 2013 JS displays: Aug 23, 2013

Note: This happens only with some users, and I cannot reproduce it locally.

Any ideas? Jared

Upvotes: 2

Views: 106

Answers (1)

John Green
John Green

Reputation: 13435

The javascript is using local machine clock, while the server is spitting out server time. Unless all of your users are in the same time zone as the server, you'll run into this issue. A solution used by many is to normalize server time to something like UTC, then make appropriate calculations on the client side javascript to account for the delta.

Upvotes: 5

Related Questions