Question

I am trying to parse a logfile, using human readable dates without a year into a timestamp. I have looked over the function strtotime() but haven't had any success with it.

Example time: Apr-26-10:49:36 which is the equiv of "M-j-H:i:s" for the date() function.

Edit: without a year, in my case here..its perfectly fine to assume the year is the current year.

I've created a script to break this down in the past, but it was long and redundant. I was hoping for a more simplified way of doing this. Any help, or pointing in the right direction would be greatly appreciated :D

Was it helpful?

Solution

Maybe not the prettiest way to do it but it works

The problem why the strtotime function is not working is because the hyphen between the Day and the Hour 26-10.

You can replace the - and then use strtotime.

echo date('Y-m-d H:i:s', strtotime(str_replace('-', ' ', 'Apr-26-10:49:36')));

OTHER TIPS

If you know the format of the date, you can use strptime to parse it. This returns an array that you can use to determine the arguments for mktime.

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