how to use NOW()

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
pradip
Forum Newbie
Posts: 5
Joined: Wed Feb 20, 2008 11:42 pm

how to use NOW()

Post by pradip »

i have used NOW() function in query but it always show 0000-00-00 00:00:00
please tell how to use this to show current date and time.
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

Re: how to use NOW()

Post by djdon11 »

it is only for mysql aactually so if u want to insert the current date than use it into the query like this

insert into tablename (currentdate) values (now());
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to use NOW()

Post by califdon »

pradip wrote:i have used NOW() function in query but it always show 0000-00-00 00:00:00
please tell how to use this to show current date and time.
Show us your query, and tell us what version of which database engine you are using.
pradip
Forum Newbie
Posts: 5
Joined: Wed Feb 20, 2008 11:42 pm

Re: how to use NOW()

Post by pradip »

I have used in query of the form:

"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username'";
with MySQL 5.0
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: how to use NOW()

Post by alex.barylski »

I personally have never used now() in fear that the results might somehow differ from that of the code most of my logic relies on...

I just initialize a variable from mktime() or time() and use string interpolation or the C style sprintf, etc...

Not saying now() is wrong...just at least when you dump the SQL to screen when debugging the timestamp value is obviously.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Re: how to use NOW()

Post by blacksnday »

pradip wrote:I have used in query of the form:

"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username' ";
with MySQL 5.0
I know this doesnt answer your question But I had to add this:
One thing you should add to that query at the end is: LIMIT 1
such as:
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username' LIMIT 1";

plus based on that query and me using mySQL4.1.22-standard I use same query basically and it works fine on my server, did mySQL change for future versions I should be aware of?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: how to use NOW()

Post by alex.barylski »

One thing you should add to that query at the end is: LIMIT 1
Why? As long as the username is unique only that record gets updated. LIMIT 1 could lead to some weird bugs and is somewhat hackish.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: how to use NOW()

Post by RobertGonzalez »

I use NOW() in all of my queries that need the now time in the format "YYYY-MM-DD HH:MM:SS" without issue.

I would run a simple select to see if your mysql server is understanding that function, and if not, to see if the server can even serve time/date data:

Code: Select all

SELECT NOW() AS `right_now`;
Post Reply