LLLLLLL Posted September 19, 2010 Share Posted September 19, 2010 It seems like this is a question that may be on here already, but I couldn't find it with a search, so I apologize. I am given a string that represents a datetime, and it is always in this format: HH:mm:ss MMM DD, YYYY PDT. I just want to turn that into a proper format for mysql (Y-m-d H:i:s), but I'm not exactly sure how to go about that. (I could do this in C# rather quickly!) Probably a simple question, I hope someone can help! Thanks. Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/ Share on other sites More sharing options...
LLLLLLL Posted September 19, 2010 Author Share Posted September 19, 2010 Note that I'm on PHP Version 5.2.14. Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/#findComment-1112753 Share on other sites More sharing options...
LLLLLLL Posted September 19, 2010 Author Share Posted September 19, 2010 I'm doing this for now. pretty ugly public function setPaymentDate( $pds ) { // pds is "pay date string", in the format of HH:mm:ss MMM DD, YYYY PDT $a = explode( ' ', $pds ); $t = explode( ':', $a[ 0 ] ); $monthText = $a[ 1 ]; $day = str_replace( ",", "", $a[ 2 ] ); $year = $a[ 3 ]; $hour = $t[ 0 ]; $min = $t[ 1 ]; $sec = $t[ 2 ]; // this is ugly. A better way? $months = array( "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12" ); $month = $months[ $monthText ]; $unixTs = mktime( $hour, $min, $sec, $month, $day, $year ); $this->payment_date = date( 'Y-m-d H:i:s', $unixTs ); } Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/#findComment-1112767 Share on other sites More sharing options...
void Posted September 19, 2010 Share Posted September 19, 2010 $time = strtotime($pds); echo date("Y-m-d H:i:s", $time); Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/#findComment-1112775 Share on other sites More sharing options...
LLLLLLL Posted September 19, 2010 Author Share Posted September 19, 2010 strtotime just knows how to parse "HH:mm:ss MMM DD, YYYY PDT"? This was unclear to me in any documentation. I was using http://www.php.net/manual/en/class.datetime.php for reference. (I haven't tried your code; I'm away from my dev environment right now.) Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/#findComment-1112777 Share on other sites More sharing options...
jcbones Posted September 19, 2010 Share Posted September 19, 2010 I ran a quick test. This code: <?php $time = '10:20:33 Jan 02, 2010 PDT'; echo date('Y-m-d H:i:s',strtotime($time)); ?> Output this data: 2010-01-02 12:20:33 Note, my server is EST. Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/#findComment-1112910 Share on other sites More sharing options...
LLLLLLL Posted September 19, 2010 Author Share Posted September 19, 2010 Excellent. Yes, I just tested this as well. thanks. Link to comment https://forums.phpfreaks.com/topic/213801-date-time-parsing-question/#findComment-1112914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.