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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.