visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Databases :: MySql :: Timestamp Question


By: Squibi U.S.A.  Date: 31/05/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
Say I have a timestamp(6) field called thedate. I query:

$query = "SELECT thedate FROM cr_gameinfo WHERE gamename = 'blabla'";
$result = mysql_db_query("cheatc1_thedb", $query) or die("bad query '$query', error : ".mysql_error());

How can I figure out how many days have gone by between the date in the timestamp field and the current date, and assign it to a variable in PHP? Thanks in advance.

By: VGR Date: 31/05/2003 07:17:00 English  Type : Answer
select TO_DAYS(now())-TO_DAYS(thedate) as a FROM cr_gameinfo WHERE gamename = 'blabla'";

// perform query, $res=mysql_fetch_array($result);

// your number of days is $res['a']
By: Squibi Date: 31/05/2003 08:26:00 English  Type : Comment
I used the following code. Nothing was printed out to the screen at all, not even an error. Can you see anything wrong with this?

$query = "select TO_DAYS(now())-TO_DAYS(thedate) as a FROM cr_gameinfo WHERE gamename = 'blabla'";
$result = mysql_db_query("cheatc1_thedb", $query) or die("bad query '$query', error : ".mysql_error());

$daysdiff = mysql_fetch_array($result);

echo "difference = $daysdiff['a']";

I don't think it makes a difference, but thedate entries in thedatabase are from today. I still think I should be getting a zero when the difference is printed out. Also, I'm confused about why in the array $res, you access the data you want with 'a'. Please explain if you can. Sorry I have so many questions, I'm learning. Thanks for your time.
By: cdillon Date: 31/05/2003 11:07:00 English  Type : Comment
The reason he uses "a" is because there is no other way to access your computed column unless you give it a name. He could have named it more descriptively like :

select TO_DAYS(now())-TO_DAYS(thedate) as daysdiff
FROM cr_gameinfo
WHERE gamename = 'blabla';

Is there any data that fits this criteria? Do you have a game in you db named "blabla"?
By: VGR Date: 01/06/2003 20:43:00 English  Type : Comment
yes, it's just convenient and concise (short)
By: Squibi Date: 01/06/2003 20:54:00 English  Type : Comment
Yes, there is an entry called blabla. What about just selecting thedate from the db and calculating the difference in PHP? Would that be possible? I don't see anything wrong with my code above, so I don't know what to do.

Thanks
By: VGR Date: 01/06/2003 21:03:00 English  Type : Comment
you can't make this :
echo "difference = $daysdiff['a']";

you've to do :
echo 'difference = '.$daysdiff['a'];
or
echo "difference = {$daysdiff['a']}";


i also suggest tou add a
to at least try to flush a line to the browser
By: VGR Date: 01/06/2003 21:04:00 English  Type : Comment
and i don't see anything wrong except perhaps the use of mysql_db_query(), deprecated since 4.0.6 :

Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.
By: Squibi Date: 01/06/2003 21:14:00 English  Type : Comment
You were correct, it was the problem with my echo statement. It works perfectly. How come my echo statement didn't work? I'm assuming its just because it was in array form, correct? Thanks for all your help.
By: VGR Date: 01/06/2003 21:39:00 English  Type : Comment
exactly. That's the only case where using an echo <<<EOS ... EOS; is not convenient and requires secondary variables.

usually, any variable reference may be passed into parsed strings, EXCEPT objects references and array elements. Thanks the Gods of Péhachepé, you can theoretically do it by enclosing the whole reference in curly brackets { }

regards

Do register to be able to answer

EContact
browser fav
page generated in 313.692090 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page