0

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????

asked May 2, 2012 at 8:40
2
  • 3
    It seems timezones are different in PHP and your browser, try setting the same one. Commented May 2, 2012 at 8:43
  • 2
    As an aside: date('2012年05月02日 17:00:00') is pointless and the same as just the string '2012年05月02日 17:00:00'. Commented May 2, 2012 at 8:47

4 Answers 4

1

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.

answered May 2, 2012 at 8:51
Sign up to request clarification or add additional context in comments.

Comments

1

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>
answered May 2, 2012 at 8:53

Comments

0

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.

answered May 2, 2012 at 8:45

Comments

0

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.

answered May 2, 2012 at 8:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.