Search code examples
phpmysqldatephp-5.2

How to convert (very) old dates in PHP?


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)


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