help needed with the date() function and loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fridday
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2008 10:45 am

help needed with the date() function and loops

Post 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!?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post by aceconcepts »

use strtotime:

Code: Select all

 
$upload_date = date("Y-m-d, H:m", strtotime($upload_timestamp));
 
fridday
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2008 10:45 am

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

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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');
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
fridday
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2008 10:45 am

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

Post 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?
fridday
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2008 10:45 am

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

Post by fridday »

Solved!!!

Y-m-d, H:m

should be

Y-m-d, H:i

Stupid me! Thanks everyone for your help!!
Post Reply