Jump to content

Date time parsing question


LLLLLLL

Recommended Posts

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

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 );	
}

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.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.