note 43999 deleted from function.strtotime by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 12 Jul 2004 10:51:28 +0000 |
| Subject: | note 43999 deleted from function.strtotime by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73091@lists.php.net to get a copy of this message | ||
Note Submitter: gun@gunandgiz.com
----
While converting a database from MS SQL Server to MySQL running on Linux, I had problems converting
Microsoft's smalldatetime field with dates where the month was < 10. Here's the
function I used to solve the problem with strtotime getting the month/day mixed up. I'm sure
there's a simpler way to convert the month from Jan to 01, but this gets the job done:
<?php
function datetotime($date) {
// MS SQL Server will return with this format: Sep 03 2003 06:01PM
list($month, $day, $year, $hour, $minute, $ampm) = sscanf($date,'%s %d %d %d:%d%s');
$monthconv = 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'
);
$monthnum = $monthconv[$month];
$datestring = "$year-$monthnum-$day $hour:$minute:00 $ampm";
$time = strtotime($datestring);
if ($time == -1) {
die("Problem with $date --> $datestring\n");
}
return $time;
}
?>