0

Querying SQL Server (MSSQL) columns in PHP (Ubuntu / PHP 5.2.4), my date columns are coming out looking like this:

May 16 2013 12:00:00:000AM

strtotime won't parse them.

Tried this:

preg_match("/^([a-zA-Z]{3}) ([0-9]{1,2}) ([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}):[0-9]{3}([apmAPM]{2})$/",$time_string,$regs);
$month = $regs[1];
$day = $regs[2];
$year = $regs[3];
$hour = $regs[4];
$minute = $regs[5];
$second = $regs[6];
$ampm = $regs[7];
$converted_time_string = $month." ".$day." ".$year." ".$hour.":".$minute.":".$second.$ampm;

$actual_time = strtotime($converted_time_string);

return $actual_time;

But it's having trouble with some dates, for example:

Aug 1 2013 12:00:00:000AM

Any more reliable suggestions on how to do this? Upgrading PHP not an option at this time.

2 Answers 2

3

I recommend you use SELECT ... CONVERT(VARCHAR(19), dateColumn, 120) ... to select the date in YYYY-MM-DD HH:MM:SS (24h) format - this assures loss-free conversion with strtotime()

Sign up to request clarification or add additional context in comments.

Comments

0

It's possible that date_parse_from_format could parse it to a associated array. With this array you can create the new timestring.

http://www.php.net/manual/en/function.date-parse-from-format.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.