DelphiFAQ Home Search:
General :: Programming :: perl :: mysql
How to work mysql from perl. This site is mainly built with mysql and perl (and some php)

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

Convert a date from Apache's string format to mysql's date format in perl

Question:

Apache logs dates in this form: [25/Apr/2005:11:45:59 -0400] and I need to insert this string in a mysql table as a datetime value.
How can I convert it?

Answer:

mysql has powerful date handling functions, which unfortunately vary from version to version substantially.
You may use the following perl code instead - it converts

25/Apr/2005:11:45:59

to

2005-04-25 11:45:59

%mo = (
  'Jan'=>'01',
  'Feb'=>'02',
  'Mar'=>'03',
  'Apr'=>'04',
  'May'=>'05',
  'Jun'=>'06',
  'Jul'=>'07',
  'Aug'=>'08',
  'Sep'=>'09',
  'Oct'=>'10',
  'Nov'=>'11',
  'Dec'=>'12');
 
 $time = substr($time,7,4) . '-' . $mo{substr($time,3,3)} . '-' . substr($time,0,2) . ' ' . substr($time,12);
 

Generated 16:01:47 on May 22, 2017