Shortest way to convert PayPal's prefered date format to MySQL datetime format in ColdFusion CF9

StackOverflow https://stackoverflow.com/questions/23102416

  •  04-07-2023
  •  | 
  •  

Question

PayPal's IPN gives dates in this format 06:52:15 Apr 12, 2014 PDT which is not a valid recognised format. Also, the timezone is specified; I can't find a ColdFusion date class that can take a timezone. So, what's the shortest way to convert the format and translate the timezone to GMT (the timezone of my server). So far I have this...

arr3LA = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dev'];

arrDateParts = ListToArray( FORM.payment_date, ' ');
arrTimeParts = ListToArray( arrDateParts[1], ':' );

arrDateParts[3] = Replace(arrDateParts[3], ',', '', 'All');

objPayPalDateTime = CreateDateTime( arrDateParts[4], ArrayFind( arr3LA, arrDateParts[2]), arrDateParts[3], arrTimeParts[1], arrTimeParts[2], arrTimeParts[3] );

strLSFriendly = DateFormat( objPayPalDateTime, 'mmmm dd, yyyy' ) & ' ' & TimeFormat( objPayPalDateTime, 'h:mm:ss tt' ) & ' ' & arrDateParts[5];

objDateTime = LSParseDateTime( strLSFriendly );

strSQLFriendlyDateTime =  DateFormat( objDateTime, 'yyyy-mm-dd hh:mm:ss' );

Can you improve on that?

Was it helpful?

Solution

I'd say forget using native CFML stuff here, and leverage java:

target = "06:52:15 Apr 12, 2014 PDT";
sdf = createObject("java", "java.text.SimpleDateFormat").init("hh:mm:ss MMM dd, yyyy zzz");
result =  sdf.parse(target);    
writeDump([{target=target},{result=result}]);

Does that do the trick?

(NB: inspired by this question: How to parse date string to Date?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top