开发者

PHP MYSQL update a DATE and TIME field in db manually

I am trying to figure out how to update a MYSQL DATE and TIME field manually (NOT to todays date!)ie set it the date field to a certain date value an开发者_运维知识库d time field to a certain time field correctly in the correct SQL required field format.

I am using an UPDATE query not INSERT as my action is to update a users field

Done some research and came up with something along the lines of (obviously this example wont work but does anyone know how to format this query?

UPDATE mytblname SET date='DATE: Manual Date', '2011-06-14'', time='TIME: Manual Time', '12:10:00' WHERE email='somevalue'

If I just enter the value as normal SQL way it gives 0000-00-00 for date and 00:00:00 for time - ie

SET date='2011-06-14',time='12:33:35'

Thanks for any suggestions, really appreciate it!!!


UPDATE mytblname SET `date`="2011-06-14", `time`="12:10:00" WHERE email="somevalue";

This should work fine. Make sure you have the appropriate backticks around date and time.


Please refer to the MySQL Documentation on DATE, TIME and DATETIME formats. You can see there, that there are multiple possibilities of the values that can be assigned to fields of these types.

So this should work:

UPDATE `mytblname` SET `date`=NOW(), `time`=NOW() WHERE `email`='somevalue';

or to any specific date like that (the string will be automatically converted to DATE, TIME or DATETIME format):

UPDATE `mytblname`
SET
`date`='1987-01-02 11:22:33',
`time`='1987-01-02 11:22:33'
WHERE `email`='somevalue';

You can also assign it like that, which is more clear:

UPDATE `mytblname`
SET
`date`='1987-01-02',
`time`='11:22:33'
WHERE `email`='somevalue';

The only question is, which path will you choose :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜