Date problem


#1

This is a little part of the code that I have.

[code] $query = "SELECT * FROM some_table";
$result = mysql_query($query);

$num = mysql_numrows($result);

for($c = 0; $c < $num; $c++) {
$date = mysql_result($result, $c, “date”);
$date = date(“F j, Y | G:i:s”, $date);
/* do some stuff here */
}
[/code]

The first time that ‘$date’ is defined…

$date = mysql_result($result, $c, "date");

…it fetches and returns the date correctly from the database:

2004-04-26 11:06:51

The second time that ‘$date’ is defined is basically to format it to look how ever I want it to look like. I put it in this format…

Month Day, Year | Hour:Minutes:Seconds

using this…

$date = date("F j, Y | G:i:s", $date);

…but instead of displaying the correct date, it displays:

December 31, 1969 | 19:33:24

All right. I tried to break it down so that you could understand what I was saying. I can’t seem to figgure out what’s wrong with it, any help?


#2

Your problem is that “2004-04-26 11:06:51” isn’t a timestamp. You are trying to use it as one in the second argument of the date function. What it expects is a unix timestamp. What it gets is the date in whatever format you’ve got it stored already.


#3

By the way, the best way to convert it is:
http://ca3.php.net/manual/en/function.strtotime.php
Which is explained a bit more here:
http://www.gnu.org/software/tar/manual/ … tar_7.html


#4

[code] $query = "SELECT * FROM some_table";
$result = mysql_query($query);

$num = mysql_numrows($result);

for($c = 0; $c < $num; $c++) {
$date = mysql_result($result, $c, “date”);
$timestamp = strtotime($date);
$date = date(“F j, Y | G:i:s”, $timestamp);
/* some stuff here */
}
[/code]

Works perfectly well, thanks alot. wwoo I’m learning :smiley:


#5

Glad I could help. Nice to see someone who is happy to be learning.