Last Updated: February 25, 2016
·
2.017K
· mariopatch

timezone - from Javascript to PHP

/*Javascript*/
    var d=new Date();
    var offset = d.getTimezoneOffset()*60;
    var timestamp = Math.ceil(d / 1000); //give this to PHP
    console.log('offset:'+offset);
    console.log('timestamp:'+timestamp);
    console.log(d.show());
/*PHP*/
    $offsetServer= date_offset_get(new DateTime); //in hour
    echo "Offset server : "; echo $offsetServer; echo "\n";

    $today = new DateTime("now");
    echo "Current date server : "; echo date('Y/m/d H:i:s', $today->getTimestamp()); echo "\n";

    $ts=1392381052;         //in minute - get by javascript
    $offsetClient=18000;    //in seconde - get by javascript
    $offset=$offsetClient+$offsetServer; //in second

    $modifiedServer=$today;
    $modifiedServer=$modifiedServer->setTimestamp($ts);
    echo "Modified date server : "; echo date('Y/m/d H:i:s', $modifiedServer->getTimestamp()); echo "\n";
    $modifiedClient=$today;
    $modifiedClient->setTimestamp($ts-$offset);
    echo "Modified date client : "; echo date('Y/m/d H:i:s', $modifiedClient->getTimestamp()); echo "\n";

1 Response
Add your response

In JavaScript, you can just do new Date() / 1000, no need to call getTime() as Date.prototype.valueOf (which gets called when you try to perform an arithmetic operation on a date object) returns a timestamp.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside