Page 1 of 1

help needed with the date() function and loops

Posted: Mon Mar 31, 2008 11:20 am
by fridday
I'm going crazy here, I just don't understand why my code don't work. Maybe I'm missing someting...

I'm building a torrent site and have an array with torrent data ($torrent_data). For each loop I make a SELECT from the database (yeah yeah I know...), then put the name of the torrent in $name and the timestamp in $upload_timestamp. Everything works so far. But when I convert the timestamp with date() or gmdate() it just convert the first date. Check the output!

Code: Select all

<?php
$i=0;
while ($i<$torrent_amount) {
   $torrent_id = $torrent_data[$i]["id"];   
   $torrent_table = mysql_query("SELECT name, date FROM torrent WHERE id='$torrent_id'");       
   $name = mysql_result($torrent_table, 0, "name");
   $upload_timestamp = mysql_result($torrent_table, 0, "date");
   $upload_date = date("Y-m-d, H:m", $upload_timestamp);
   print $torrent_name.", ";
   print $upload_timestamp.", ";
   print $upload_date."<br>";
   $i++;
   }
?>
output:
torrent1, 1206961055, 2008-03-31, 12:03
torrent2, 1206957978, 2008-03-31, 12:03
torrent3, 1206959500, 2008-03-31, 12:03

strange thing is that $torrent_name and $upload_timestamp is redeclared as they should each loop, but not $upload_date. Anyone knows why!?

Re: help needed with the date() function and loops

Posted: Mon Mar 31, 2008 11:24 am
by aceconcepts
use strtotime:

Code: Select all

 
$upload_date = date("Y-m-d, H:m", strtotime($upload_timestamp));
 

Re: help needed with the date() function and loops

Posted: Mon Mar 31, 2008 11:32 am
by fridday
I thought it worked at first, but I tried it on an old database with string parsed dates. When I applied it to the current db with unix timestamps I can't get it to work. Now output is:

torrent1, 1206961055, 1970-01-01, 01:01
torrent2, 1206957978, 1970-01-01, 01:01
torrent3, 1206959500, 1970-01-01, 01:01

Re: help needed with the date() function and loops

Posted: Mon Mar 31, 2008 12:47 pm
by s.dot
Where is $torrent_amount set at?

Anyways, you are only checking the first row within mysql_result() :)

You'll want to replace '0' with $i

Code: Select all

$upload_timestamp = mysql_result($torrent_table, $i, 'date');

Re: help needed with the date() function and loops

Posted: Mon Mar 31, 2008 1:20 pm
by fridday
Thanks for helping out! But no, that's not quite it... I'm doing a new SELECT for every loop, for different reasons.. Try the code below. I have removed the database-stuff to show you what's going on.

Code: Select all

<?php
$torrent_data[0]["name"] = "torrent one";
$torrent_data[1]["name"] = "torrent two";
$torrent_data[2]["name"] = "torrent three";
$torrent_data[0]["unix_timestamp"] = 1206961055;
$torrent_data[1]["unix_timestamp"] = 1206957978;
$torrent_data[2]["unix_timestamp"] = 1206959500;
$i=0;
while ($i<3) {
   $torrent_name = $torrent_data[$i]["name"];
   $torrent_timestamp = $torrent_data[$i]["unix_timestamp"];
   $torrent_date = date("Y-m-d, H:m", $torrent_timestamp);
   print $torrent_name.", ";
   print $torrent_timestamp.", ";
   print $torrent_date."<br>";
   $i++;
   }
?>
output:
torrent one, 1206961055, 2008-03-31, 12:03
torrent two, 1206957978, 2008-03-31, 12:03
torrent three, 1206959500, 2008-03-31, 12:03

Isn't it strange?

Re: help needed with the date() function and loops

Posted: Mon Mar 31, 2008 2:50 pm
by fridday
Solved!!!

Y-m-d, H:m

should be

Y-m-d, H:i

Stupid me! Thanks everyone for your help!!