Page 1 of 1

issue displaying time (issue in converting)

Posted: Thu Sep 18, 2003 5:40 pm
by m3rajk
than to jam's suggestion on a different issue that this seems realtated to, i changed some code and made it better, so now i KNOW i have the timestamp. the problem is converting it to the user's time.

the code in question:

Code: Select all

function getthreads($fid, $db, $utds, $gmtos){ # get the threads in a forum
  include("/home/joshua/includes/fyd.altincs.php"); # includes file for functions
  $select='thread_id,curr_fid,sticky,locked,title,thread_auth,UNIX_TIMESTAMP(made),last_post_auth,UNIX_TIMESTAMP(last_post_time),amt_posts,orig_fid,post1id';
  $findrthreads=mysql_query("SELECT $select FROM threads WHERE curr_fid='$fid' AND sticky='0' ORDER BY made DESC", $db); # find regular threads
  $findsthreads=mysql_query("SELECT $select FROM threads WHERE curr_fid='$fid' AND sticky>'0' ORDER BY made DESC", $db); # find sticky threads
  $numrthreads=mysql_num_rows($findrthreads); #regular threads
  $numsthreads=mysql_num_rows($findsthreads); # stickied threads (will be added once i have display working. max or 10 for a total of 30 to a page)
  $numthreads=$numrthreads+$numsthreads; # how many threads
  $threads=''; # threads to return
  $page=1; # page we're on
  if(isset($_GET['page'])){ $page=$_GET['page']; } # set the page
  if($numthreads===0){ # no threads
    $threads='    <tr><td colspan="5">There are no posts in this Forum</td></tr>';
  }else{ # there's threads

    # flength (forum length)... set in includes. currently 30
    $start=(($flength*$page)-$flength); # set start
    $end=$flength*$page; # get initial end
    if($end>$numrthreads){ $end=($numrthreads-($flength*($page-1))); } # set end

    if($start!=0){ $jump=mysql_data_seek($findrthreads, $start) or die('database error seeking threads'); } # jump to the first thread (if needed)

    for($i=0;$i<$end;$i++){ # for each thread we're gonna show

      $threadinf=mysql_fetch_array($findrthreads); # get thread info
      $tid=$threadinf['thread_id']; # thread id
      $thread=$threadinf['title']; # get title
      $started=$threadinf['made']; # when was it made (add later)
      $posts=$threadinf['amt_posts']; # how many posts in thread?
      $lpa=$threadinf['last_post_auth']; # who made the last post
      $lpt==$threadinf['last_post_time']; # when was the last post
      $lock='<img src="sitepics/unlocked.png">'; # set default locked

      if($threadinf['locked']){ $lock='<img src="sitepics/locked.png">'; } # if it's locked, change lock

      echo "<br />$lpt :: $utds :: $gmtos\n"; # error reporting (last post time :: user's time display style :: gmt offset)
      $lpt=gmmktime($utds, ($lpt+($gmtos*60*60))); # change to user's time format/zone
      echo "<br />$lpt\n"; # error reporting (is it right?)

      $threads.="                 <tr><td>$lock</td><td><a href="forums.php?fid=$fid&tid=$tid">$thread</a></td><td align="center">$posts</td><td>$lpa</td><td>$lpt</td></tr>\n";  #make thread listing
    }
  }
  return $threads; # return thread list
}
and what i got from mysql directly:
mysql> select thread_id, UNIX_TIMESTAMP(last_post_time) from threads;
+-----------+--------------------------------+
| thread_id | UNIX_TIMESTAMP(last_post_time) |
+-----------+--------------------------------+
| 1 | 1063604865 |
| 2 | 1063605060 |
| 3 | 1063605206 |
| 4 | 1063605334 |
| 5 | 1063605380 |
+-----------+--------------------------------+
5 rows in set (0.00 sec)

mysql>
there's two echo statements to help in this. the print out i got from it was:
:: m/d/Y H:i:s :: -5.00
1062763236
1062763236 :: m/d/Y H:i:s :: -5.00
-14401
-14401 :: m/d/Y H:i:s :: -5.00
1061899176
1061899176 :: m/d/Y H:i:s :: -5.00
-14401
-14401 :: m/d/Y H:i:s :: -5.00
1061899176

Posted: Thu Sep 18, 2003 6:13 pm
by matthiasone
Maybe I am missing something, but wouldn't the date() function to user time? http://us2.php.net/manual/en/print/function.date.php

Posted: Fri Sep 19, 2003 1:19 pm
by m3rajk
no. date() is the local time to the SEVER. to get the USER regaurdless of timezone, you need to make a time using the GMT offset of the user and the the GMT timestamp of when it was posted.

Code: Select all

gmmktime($utds, ($lpt+($gmtos*60*60)));
is how that's done. i store all times GMT so $lpt is pulled from my db as a timestamp. (GMT timestamp) add the GMT offset ($gmtos) in seconds ($gmtos*60*60) and use the user's choice of time display ($utds)


ahh crap. i just saw the issue

when looking back to show you that i pull it fromt he db as it's suppossed to be i noticed what's wrong. i was just too close to the code

Posted: Fri Sep 19, 2003 3:06 pm
by m3rajk
there was another issue.by requesting it form the database as UNIX_TIMESTAMP(last_post_time) i need to get it from the array that way