note 90055 added to function.strptime

From: Date: Fri, 03 Apr 2009 13:33:32 +0000
Subject: note 90055 added to function.strptime
Groups: php.notes 
Request: Send a blank email to php-notes+get-152792@lists.php.net to get a copy of this message
If you are just looking to switch an existing time format into a mysql compatible format(like YYYYMMDDHHMMSS.uuuuuu) you should use STR_TO_DATE. see: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html However, if you have an old version of mysql (before 4.1), you may want to consider using a regex. The example below doesnt have seconds because they arent used often if you are ripping data from a web page. You will probably have to customize this to your needs. ==code== // for 24 hour times <?php $before = "12/15/2005 15:47";\ $after = preg_replace("/([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2})/", "$3$1$2$4$5", $before)."00.000000"; echo "$before\n$after\n"; ?> ==output== 12/15/2005 15:47 20051215154700.000000 . If you are unlucky, you will need to be converting to the 24 hour format as well. ==code== // for 12 hour AM/PM times <?php $before = "12/15/2005 03:47PM"; // note: you can use an anonymous function instead IF you have php 5 function convert_time($m) { if($m[6]=="PM") { $m[4]+=12; } return $m[3].$m[1].$m[2].$m[4].$m[5]."00.000000"; } $after = preg_replace_callback("/([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2})(AM|PM)/", "convert_time", $before); echo "$before\n$after\n"; ?> ==output== 12/15/2005 03:47PM 20051215154700.000000 ---- Server IP: 64.71.164.2 Probable Submitter: 68.230.177.153 ---- Manual Page -- http://www.php.net/manual/en/function.strptime.php Edit -- https://master.php.net/note/edit/90055 Del: integrated -- https://master.php.net/note/delete/90055/integrated Del: useless -- https://master.php.net/note/delete/90055/useless Del: bad code -- https://master.php.net/note/delete/90055/bad+code Del: spam -- https://master.php.net/note/delete/90055/spam Del: non-english -- https://master.php.net/note/delete/90055/non-english Del: in docs -- https://master.php.net/note/delete/90055/in+docs Del: other reasons-- https://master.php.net/note/delete/90055 Reject -- https://master.php.net/note/reject/90055 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152792) next »