use php pdo to update datetime column with current time using mysql

Posted on 2013-10-19
Medium Priority
Last Modified: 2013-10-20
How would this be done?
Question by:rgb192
  • 2
LVL 111

Accepted Solution

Ray Paseur earned 1600 total points
ID: 39585478
If you want the "current time" do not use a DATETIME column.  Instead use a TIMESTAMP column.  The column will record the latest time that the row was changed, no matter what column was changed.  UPDATE queries that do not change any data will not change the TIMESTAMP column.

If you really want to update a DATETIME column, make a string variable with date('c') and use the SQL UPDATE query to change the value in the column.
LVL 12

Assisted Solution

zappafan2k2 earned 400 total points
ID: 39585549
Ray's suggestion is certainly a much better solution.  However, not all RDBMs support the same type of timestamp data type, so you may want to keep the datetime field if you ever need to migrate.

That said, to further the last sentence of Ray's answer, you don't even need to use PHP's date function; you can simply use MySQL's now() function instead.
UPDATE mytable SET myDateTimeField = now() WHERE <conditions>

Open in new window


Author Closing Comment

ID: 39585876
Ray's idea is the best better than I thought when asking question
and the zappafan2k2 is the answer to the question.
LVL 111

Expert Comment

by:Ray Paseur
ID: 39586075
Thanks for the points.  Be aware that PHP and MySQL use different time-keeping strategies.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question