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????
4 Answers 4
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.
Comments
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>
Comments
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.
Comments
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.
date('2012年05月02日 17:00:00')is pointless and the same as just the string'2012年05月02日 17:00:00'.