Databases :: MySql :: Timestamp Question |
|||
| By: Squibi |
Date: 31/05/2003 00:00:00 |
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 | 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 | 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 | 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 | Type : Comment |
|
| yes, it's just convenient and concise (short) |
|||
| By: Squibi | Date: 01/06/2003 20:54:00 | 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 | 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 | 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 | 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 | 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








