Previous: Intro to Programming PIC Microcontrollers in Linux
Next: Gnome Programming: Getting Started with Anjuta/Glade

PHP Format Any Time/Date for MySQL

April 20th, 2005

Many people find the act of formatting a date or time for input into a MySQL database tedious. It's actually very simple. Here is a function that will take a timestamp value such as time() or a human readable string and format it for MySQL. MySQL format is: 'YYYY-MM-DD HH:MM:SS'

The output of this little script would look something like:

2005-04-20 21:54:00
2000-09-10 04:30:00
2000-09-15 00:00:00

[code lang="php"]2008-08-20 01:53:13
2000-09-10 04:30:00
2008-08-21 00:00:00
[/code]



Categories
Web Development PHP

Related Posts


Technorati Tags

2 Responses to “PHP Format Any Time/Date for MySQL”

RSS Subscription Comments RSS Feed

  1. Uthman Says:

    strtotime between php4 and php5 behavior differently; also the way older mysql stores datetime/timestamp (14) vs the new format YYYY-mm-dd HH:ii:ss, will cause further confusion if using strtotime. i would stay away from it unless i was sure the platform im developing on has the newest mysql and php

  2. Micah Says:

    I did not know that. I wrote this using PHP 4 and MySQL 4 some time ago. I'll look into that. Let's call this a simplifies solution for smaller-scale projects where as you mentioned, you know the behavior of the PHP and MySQL versions being used.

Leave a Reply