Pathik Gandhi
Pathik Gandhi

Reputation: 1344

Strange issue in date in javascript and php

i m confuse in the date in javascript and php.

<?php 
$mydate = date('2012-05-02 17:00:00'); 
echo 'Today PHP  --'.$mydate;
$mytimstamp = strtotime($mydate);
echo '<br/>My PHP unix time stamp --'.$mytimstamp;
echo '<br/>'; ?>
<script type="text/javascript">
document.write('My Javascript unix time stamp --'+new Date(Number('<?php echo $mytimstamp;?>')*1000));
</script>

OUTPUT

Today PHP -- 2012-05-02 17:00:00
My PHP unix time stamp --1335978000
My Javascript unix time stamp --Wed May 02 2012 22:30:00 GMT+0530 (India Standard Time) 

Why i m getting different time in javascript????

Upvotes: 0

Views: 162

Answers (4)

Chintan
Chintan

Reputation: 1204

if you do not change your timeZone than change your code like

<script type="text/javascript">
        var d = new Date();
        var offset = d.getTimezoneOffset();
        document.write('My Javascript unix time stamp --'+new Date(Number(<?php echo (intval($mytimstamp));?> + offset*60 )*1000));
</script>

Upvotes: 1

Tadeck
Tadeck

Reputation: 137320

You are getting the same time, except in different time zones.

On PHP side you have a GMT time, and on JavaScript side you have India time.

It looks you are from India. Just make sure you understand the concept of time zones and you store the time with time zone information or in GMT/UTC time. This way you should avoid issues with incorrectly using timestamps from different time zones. Displaying such time in the form suited for user's time zone becomes trivial if you know the time zone in which it was generated.

Upvotes: 1

bitoshi.n
bitoshi.n

Reputation: 2318

In the javascript is depend on your browser. You would better decode date format on your javascript. You will get another different date on your different browser.

Upvotes: 0

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25435

Your PC must have a different timezone set to that of the server running the PHP code.

The PHP date/time is calculated using the setting for the server whereas the JavaScript date/time is calculated using the clients PC settings.

Upvotes: 0

Related Questions