Jump to content

Archived

This topic is now archived and is closed to further replies.

websteve

Dates

Recommended Posts

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

Thanks

- Steve

Share this post


Link to post
Share on other sites
(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) [snapback]386848[/snapback][/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 [b]$todays_date = date ("Y-m-d H:i:s");[/b], but they just don't work or - is there is an easier way?

Thanks

- Steve
[/quote]

Share this post


Link to post
Share on other sites
[!--quoteo(post=386848:date=Jun 22 2006, 11:25 AM:name=websteve)--][div class=\'quotetop\']QUOTE(websteve @ Jun 22 2006, 11:25 AM) [snapback]386848[/snapback][/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 [b]$todays_date = date ("Y-m-d H:i:s");[/b], 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 [b]NOW()[/b].

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

Share this post


Link to post
Share on other sites
[!--quoteo(post=386864:date=Jun 22 2006, 11:50 AM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 22 2006, 11:50 AM) [snapback]386864[/snapback][/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 [b]NOW()[/b].

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\" /]

Share this post


Link to post
Share on other sites
[!--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:

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

Share this post


Link to post
Share on other sites
[!--quoteo(post=387123:date=Jun 23 2006, 07:42 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 23 2006, 07:42 AM) [snapback]387123[/snapback][/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:

[code]$query = " ... whatever your query is ...";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // useful message[/code]
[/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

Share this post


Link to post
Share on other sites
[!--quoteo(post=387149:date=Jun 23 2006, 09:19 AM:name=websteve)--][div class=\'quotetop\']QUOTE(websteve @ Jun 23 2006, 09:19 AM) [snapback]387149[/snapback][/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:[code]INSERT INTO my_table VALUES(NULL,NOW(),'some value');[/code]
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().)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.