The Easiest Way to Add a Date From PHP to MySQL

Often you will be required to store a date inthat will by run by your database server.
MySQL. Either upon an update of an existingBelow you will find a quick method to generate
record, or on a fresh insert of a new record.the date value for the two types of MySQL field
Good programmers are particular about the typetypes: date, and datetime.
and quality of data that is stored in a MySQLStep 1. Create a field in your database that is
database. Sometime you do not want the date toeither date or datetime.
be created by MySQL at the time of the insert- If the field is of type date, it will have this
or update. Rather, you want the date to beformat: [Year-Month-Day].
created in PHP during code execution. Although- If the field is of type datetime, it will have this
the time difference from the date creation couldformat: [Year-Month-Day Hour:Minute:Second]
be well under a few milliseconds, there is aStep 2. After you have created the SQL to be
difference. This difference could add up if severalused in the MySQL Query, populate the date field
transactional sequences must happen.with either the variable $now or $today. Doing so,
A simple alternative is to create the date in PHPwill match the insert or update date/time field in
and then transfer that information to the queryPHP, to that used in your MySQL database.