tags: apache   archive   business   charity   climbing   comic   communication   database   email   exchange   family   fm2008   hack   hardware   humour   linux   liverpool   microsoft   money   mysql   network   oes   opensource   outlook   php   pictures   process   project   quote   real_life   review   rss   science   security   software   thought   tsm   updates   webdev   website   windows  

String to time

Tue, 09 Oct 2007 15:43:55
Changing a date format with PHP. Today I had to change a load of rows which had the date stored as 'd M Y H:i:s' to the mysql DATETIME format of 'Y-m-d H:i:s'. The way I went about this was using the http://www.php.net/strtotime strtotime function within PHP.

0 <?php
1 $dateoldformat = "08 Feb 2007 15:48:13";
2 $timestamp = strtotime($dateoldformat);
3 $datenewformat = date('Y-m-d H:i:s', $timestamp);
4 echo $datenewformat;
5 ?>

0 open tag
1 set the variable we'll be working with
2 use the 'settotime' function to convert the date and time into a unix stamp
3 define new variable, open date tag, first specify the format we want, then the varible to convert, close date function.
4 have a look at our new date
5 close tag

You can now setup your MySQL query to update your records with your new DATETIME.

Cheers
Ryan Partington