PDA

Click to See Complete Forum and Search --> : Date() Function Behaving strange... -[RESOLVEDish]-


Electroman
Nov 14th, 2004, 09:57 AM
In my login file I have this code to add an entry to my login table.// Create an entry to the login table...
$query = "INSERT INTO `login` (`user_id`, `staff`, `guid`, `lastactive`) ";
$query.= "VALUES (".$UserID.", 'no', '".$UserGUID."', '".date("Y-m-d H:i:s" ,time())."')";
$results = mysql_query($query);When it is run and I see what the date/time was in the database I get:
2000-11-00 44:68:93

So I then Added in theses lines just after the above: $Date = date("Y-m-d H:i:s" ,time());
echo "date(\"Y-m-d H:i:s\" ,time()) = $Date";The output and data in database was:
2004-11-14 07:41:41

So I then removed these two debug lines and the next time the entry to the database was:
2000-11-00 44:70:74


I can't understand how those two lines could possibly affect the SQL that is done before it :confused:.


Update: I have tested this a few times since posting this and its the echo statement, if I take it out its the wrong time if I put it in its the right time, but I get errors later on cos I still have header info to send.

visualAd
Nov 14th, 2004, 10:25 AM
I would abandon use of the date() function, because all that can de done in MySql query:

$query = "INSERT INTO `login` (`user_id`, `staff`, `guid`, `lastactive`) ";
$query.= "VALUES ('$UserID', 'no','$UserGUID',CURDATE());";

Electroman
Nov 14th, 2004, 10:31 AM
I thank you again Mr. Ad :D. BTW in SQl when I use the CURDATE() can I just add onto it like the php equivelant? I.E. if I do CURDATE()+60 that would be one minute?

visualAd
Nov 14th, 2004, 10:36 AM
That won't work unless you use a UNIX timestamp. You can use a UNIX timestamp, just set the field to an unsigned integer data type. In my opinion its easier if you are going to be doing arithmetic on the dates to use them, becuase you don't waste time and resources converting them.

Here is what your query would look like:

$query = "INSERT INTO `login` (`user_id`, `staff`, `guid`, `lastactive`) ";
$query.= "VALUES ('$UserID', 'no','$UserGUID',UNIX_TIMESTAMP() + 60);";


You can also go down the route of using the MySql date add function, but again that mean un-necessary conversions. But just in case here are a list of all the date function in MySql:
http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html


Nice to see you using a real programming language. VB sux0r ;)

** waits for a debate to start on whether or not PHP is a real programming lanauge **

CornedBee
Nov 14th, 2004, 11:33 AM
** waits for a debate to start on whether or not PHP is a real programming lanauge **
We're in the PHP forum. Who would debate it?

Different debate: why do you have a huge annoying text message in your sig? ;)

visualAd
Nov 14th, 2004, 11:46 AM
Originally posted by CornedBee
Different debate: why do you have a huge annoying text message in your sig? ;)
:eek: - I set the size wrong..

Should have been 1, put 11 instead :rolleyes: