chrysst
chrysst

Reputation: 357

timeago time in different timezones in javascript

I am using the timeago javascript plugin to compute the time passed from the time that a user has created a post. The problem is that when a user creates a new post the timeago function is starting shows the time passed from '10 hours' instead of 'just now'. I think this is because the time in my database is stored in different time-zone from the user's time-zone.

Any idea how to solve this problem?

After some research I find that the format of the timestamp must be in ISO 8601 but after testing is not working!

Below is the code where I am showing the time.

<?php
foreach ($postsArray  as $v1) 
                            {
                            ?>      
                            <article>                       
                            <header><h3><?php echo $v1->TITLE; ?></h3>            </header>
                            <p><?php echo $v1->CONTENT; ?></p>
                            <?php
                            $timestamp = $v1->TIME;
                            $formatedDate = date("c",strtotime($timestamp));
                            ?>
                            <footer><b>Author:</b> <?php echo $v1->USERNAME; ?> <b>Published:</b> <time class="timeago" datetime="<? echo $formatedDate; ?>"></time></footer>
                            </article>
                            <?php
                            }
                            ?>    

Below is the javascript function:

<script type="text/javascript">
  $(document).ready(function(){
  $('time.timeago').timeago();
    });
</script>

Upvotes: 1

Views: 1954

Answers (2)

Ryan McGeary
Ryan McGeary

Reputation: 239914

Author of jquery-timeago here.

As the docs reference, an ISO 8601 timestamp is required for everything to work as expected in timeago (time zones are automatically handled):

<time class="timeago" datetime="2008-07-17T09:24:17Z">July 17, 2008</time>

In PHP, to get an ISO 8601 timestamp, here's an example snippet:

$time->format(DateTime::ISO8601);

Upvotes: 2

bandris
bandris

Reputation: 62

I prefer moment js :) http://momentjs.com/timezone/

You can check the db timestamp and the data whaz you can get on server an client side. Compare it and you can find where is the wrong timezone.

Upvotes: -1

Related Questions