Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp between Javascript and PHP

Javascript:

I have object cell with something date

params.date = cell.getDate();
params.timestamp = cell.getDate().getTime() / 1000;
console.log(params);

Object {date: Thu May 09 2013 00:00:00 GMT+0800 (China Standard Time), timestamp: 1368028800}

Then I try to check timestamp in PHP

$date = '1368028800';
echo date('Y-m-d', $date);

2013-05-08

Difference in one day. Why?

like image 545
indapublic Avatar asked Mar 24 '13 00:03

indapublic


People also ask

Is JavaScript timestamp in milliseconds?

getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

What is timestamps in JavaScript?

The timeStamp event property returns the number of milliseconds from the document was finished loading until the specific event was created.

How to fetch Date from Datetime in PHP?

getDate(parameter); Parameter The parameter is optional as it takes the current local time as default parameter. Return Type It returns the information of the date, day, year, month etc in an array.


2 Answers

When you get timestamp from Javacript date object :

it output will be interms of milli-seconds

 <script>
        var d = new Date();
        alert(d.getTime());

     </script>

output : 1386746353000

Where as php date object timestamp interms of seconds

<?php 
        $date = new DateTime();
        echo $current_timestamp = $date->getTimestamp();
     ?>

output : 1386746353

So when you are going to use javascript date object timestamp with php date object you should divide timestamp of javascript by 1000 and use it in php

like image 106
muni Avatar answered Oct 17 '22 20:10

muni


params.date = cell.getDate();

Returns the DATE not the TIME.

params.timestamp = cell.getDate().getTime() / 1000;

is converting the date into a date+time - not reading the current time.

But even if you get the timestamp in javascript, the output of PHP's date function will depend on what timezone it is in.

like image 11
symcbean Avatar answered Oct 17 '22 20:10

symcbean