Format a RFC2822 date for mysql datetime fields
I was crash-coursing myself on PHP’s IMAP functionality recently, one of the first questions I came across was how I might store the date from an e-mail header in a MySQL DATETIME field. I was afraid I was going to have to parse out the string using a bunch of calls to substr(), but then I remembered that the strtotime() function recognizes various RFC formats. So, if you need to get the timestamp of an RFC2822 e-mail header—or most any RFC-based timestamp—into MySQL DATETIME format, it’s easy: [code=“php”]$timestamp = strtotime(‘Tue, 30 Sep 2008 10:30:00 EDT’);
echo date(‘Y-m-d g:i:s a’, $timestamp);[/code] Keep in mind that if there are differences between the timezones of your server and the timestamp, the date() function could screw with your desired output. You should probably take a look at date_default_timezone_set().
comments powered by Disqus