pillmill
asked on
convert apache log date time to mysql?
How can I convert the apache log date time,
such as, 26/Apr/2011:13:21:58, to a format
that can be stored in a mysql datetime field?
such as, 26/Apr/2011:13:21:58, to a format
that can be stored in a mysql datetime field?
Try this: http://us2.php.net/manual/en/function.strtotime.php
ASKER
Thanks, but strtotime() did not work. Also tried mysql str_to_date.
Any other possibilities?
Any other possibilities?
Are you certain it is coming over in that format OR is it a UNIX timestamp?
i.e., have you tried using MySQL's FROM_UNIXTIME() function on the value being passed in?
What format did you use with strtotime and/or str_to_date when you tried? I suspect the issue is the '/' typically gets interpreted with US format which is m/d/y as you will see in the documentation provided by yodercm; therefore, it likely won't know what to do with 26/Apr/2011 by default.
i.e., have you tried using MySQL's FROM_UNIXTIME() function on the value being passed in?
What format did you use with strtotime and/or str_to_date when you tried? I suspect the issue is the '/' typically gets interpreted with US format which is m/d/y as you will see in the documentation provided by yodercm; therefore, it likely won't know what to do with 26/Apr/2011 by default.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can try this for PHP strtotime format "j/M/Y:H:i:s"
If you want the full brute-force method to do this, here it is.
<?php
$date="26/Apr/2011:13:21:58";
$monthnum = 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"
);
$array1 = explode('/',$date);
$array2 = explode(':',$array1[2]);
$day=$array1[0];
$month=$monthnum[$array1[1]];
$year=$array2[0];
$hour=$array2[1];
$minute=$array2[2];
$second=$array2[3];
$mysqldate = "$year"."-"."$month"."-"."$day"." "."$hour".":"."$minute".":"."$second";
echo $mysqldate;
exit;
?>
By the way, that code works if Aug 4th is "04/Aug/...". If you might have "4/Aug/...", then you will need to check for that and add a leading zero.
http://www.laprbass.com/RAY_temp_pillmill.php
Outputs the ISO-8601 DATETIME string:
string(25) "2011-04-26T13:21:58-05:00 "
Outputs the ISO-8601 DATETIME string:
string(25) "2011-04-26T13:21:58-05:00
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');
$str = '26/Apr/2011:13:21:58';
$new = convert_log_time($str);
var_dump($new);
// A FUNCTION TO CONVERT THE STRING
function convert_log_time($s)
{
$s = preg_replace('#:#', ' ', $s, 1);
$s = str_replace('/', ' ', $s);
if (!$t = strtotime($s)) return FALSE;
return date('c', $t);
}