Jump to content


Photo

Dates


  • Please log in to reply
6 replies to this topic

#1 websteve

websteve
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 June 2006 - 03:25 PM

Can someone tell me the proper PHP syntax for Inserting into a MySQL DateTime field? I tried numeous variations, including $todays_date = date ("Y-m-d H:i:s");, but they just don't work or - is there is an easier way?

Thanks

- Steve

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 22 June 2006 - 03:48 PM

(This is not the way others do it)

I make my date field varchar so I can do it like your doing it...

try using now()



[!--quoteo(post=386848:date=Jun 22 2006, 11:25 AM:name=websteve)--][div class=\'quotetop\']QUOTE(websteve @ Jun 22 2006, 11:25 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Can someone tell me the proper PHP syntax for Inserting into a MySQL DateTime field? I tried numeous variations, including $todays_date = date ("Y-m-d H:i:s");, but they just don't work or - is there is an easier way?

Thanks

- Steve
[/quote]


#3 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 22 June 2006 - 03:50 PM

[!--quoteo(post=386848:date=Jun 22 2006, 11:25 AM:name=websteve)--][div class=\'quotetop\']QUOTE(websteve @ Jun 22 2006, 11:25 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Can someone tell me the proper PHP syntax for Inserting into a MySQL DateTime field? I tried numeous variations, including $todays_date = date ("Y-m-d H:i:s");, but they just don't work or - is there is an easier way?
[/quote]

That looks like it should have worked. (At least that format works for me in MySQL client when I just checked it.) Are you quoting the date when you insert it? (You should.) If you just want the current date/time, you can use MySQL's function NOW().

Can you give us an idea of what error(s) you're getting or what your SQL looks like?
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#4 websteve

websteve
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 23 June 2006 - 02:35 AM

[!--quoteo(post=386864:date=Jun 22 2006, 11:50 AM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 22 2006, 11:50 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
That looks like it should have worked. (At least that format works for me in MySQL client when I just checked it.) Are you quoting the date when you insert it? (You should.) If you just want the current date/time, you can use MySQL's function NOW().

Can you give us an idea of what error(s) you're getting or what your SQL looks like?
[/quote]

Thanks for the responses. The error is programmer generated using the die() function. I just have it saying "Cannot update table". I like the idea of using the MySQL NOW() function, but I'm having problems with the correct syntax. I poked around the Internet for how to use this funciton and it only served to get me more confused? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 June 2006 - 11:42 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]The error is programmer generated using the die() function. I just have it saying "Cannot update table".[/quote]
Is there any reason why you can't change that - at least for testing purposes - so that it provides truly useful information? Something like this:

$query = " ... whatever your query is ...";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // useful message


Legend has it that reading the manual never killed anyone.
My site

#6 websteve

websteve
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 23 June 2006 - 01:19 PM

[!--quoteo(post=387123:date=Jun 23 2006, 07:42 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 23 2006, 07:42 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Is there any reason why you can't change that - at least for testing purposes - so that it provides truly useful information? Something like this:

$query = " ... whatever your query is ...";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // useful message
[/quote]

I subsutituted in the format you gave and got a more specific error response. It turns out I had a syntax error. Not noticable in the query, but I realized I was going right into this url, forgetting that the variable was set in a cookie in a previous page. If you don't access that page first (which is what I have it designed to do when the users login, then when I run the update query in the next page, it wouldn't be able to assign the variable to the field. Now, it works. Thanks for that.

If you wouldn't mind and set me on the right course using the NOW() function as you mentioned before, I would like to change to using this, because I see that the date is more humanly legible, however, I am not sure how to properly syntax it?

Any help would be greatly appreciated!

Thanks

- Steve



#7 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 23 June 2006 - 02:26 PM

[!--quoteo(post=387149:date=Jun 23 2006, 09:19 AM:name=websteve)--][div class=\'quotetop\']QUOTE(websteve @ Jun 23 2006, 09:19 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]

If you wouldn't mind and set me on the right course using the NOW() function as you mentioned before, I would like to change to using this, because I see that the date is more humanly legible, however, I am not sure how to properly syntax it?
[/quote]

Do you have access/do you use the mysql client program? It's very helpful to try your SQL queries directly without going through PHP first, especially while you're learning MySQL.

The NOW() function just returns the current date/time. For a date/time column in a table it would be used like this:
INSERT INTO my_table VALUES(NULL,NOW(),'some value');
That would insert the current date and time into the second column in this example. Notice that you don't quote it -- that's reserved for strings or inserting dates manually ("2006-06-30 12:00:00").

(The MySQL manual should be helpful, too. See that for a more thorough discussion of date and time functions including NOW().)
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users