[PHP] Conversion to MySQL Datetime
MySQL supports DATE, DATETIME, and Timestamp data types. Format for DATETIME can be YYYY-MM-DD HH:MM:SS, YY-MM-DD HH:MM:SS, YYYYMMDDHHMMSS, and YYMMDDHHMMSS. MySQL is quite flexible with delimiter, for example, 98@12@31 11^30^45 is perfectly valid.
Quite often, user may input datetime in a certain format, your application may be manipulating Unix timestamp, and you save the datetime (in DB) in another format. strtotime is a flexible PHP function for converting user input to Unix timestamp. It can accept strings like “tomorrrow”, “20090831″, as parameter. It returns FALSE when it cannot parse the input parameter.
Another useful PHP function is date. Usually I use it to reformat the Unix timestamp to a nicer format for output, and also for saving into MySQL database.
This piece of code converts user’s input into a format suitable for saving into MySQL database. MySQL also has a number of useful functions for manipulating datetime.