Question

I have to migrate an Access database to MySQL. I have two problems: first is year is specified with two digits; second is that this is a birthday database. For example I have strings like this:

"06/12/76 00:00:00"
"10/15/02 00:00:00"

Which year is refering last one (October 15th)? I'll assume that all dates which have a year value over 12 are refering to 19XX and the other (from 0 to 12) to this century: 20XX.

Once resolved this problem I need to format these dates. I've try this:

$bd = strtotime($birth);
if ($bd > time()) {
    $bd = strtotime("-100 years", $bd);
}
$birth = sua_date_unix2mysql($bd);

But the problem is strtotime only is able to manage dates after 1970. Any idea to resolve this problem?

NOTE: Datetime is not available (version 5.2)

Was it helpful?

Solution

Just manipulate those dates as strings.

// "06/12/76 00:00:00"
list($month, $day, $year) = explode('/', $birth);
// $year is actually "76 00:00:00"...
$year = (int)$year;

$century = 2000;
if (($century + $year) > date('Y'))
    $century = 1900;
$year += $century;
$mysql = "$year-$month-$day"; // 19760612 will be recognized by MySQL

OTHER TIPS

I don't know if it is fitting, but you can do this with string manipulation pretty easily.

First parse the incomming string to the values. For parsing the string, you can make use of sscanf.

$date = '06/12/76 00:00:00';
$result = sscanf(
    $date, '%d/%d/%d %d:%d:%d', 
    $month, $day, $year, $hour, $minute, $second
);

Then expand the year from two letters to four letters. That should be fairly easy, a simple if.

$year += $year < 36 ? 2000 : 1900

Then re-arrange the strings to the date-format you need for mysql. For concatenating the string you can make use of sprintf.

$iso = sprintf(
    '%04d-%02d-%02d %02d:%02d:%02d', 
    $year, $month, $day, $hour, $minute, $second
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top