cwarn2’s classic function handles dates pre-1970

I would like to share a neat decent function to handle the dates pre-1970 which normally would find getting reset to 1970, January 01. I had a need where I need to handle birthdates pre-1970 and PHP’s in built functions like strtotime (), date(), mktime() , including safestrotime() of no help.

Thanks to the patience and effort of cwarn2, whom I met at Daniweb, I now have a function that address my need. This post is an acknowledgment to cwarn2 friendly mind. Thanks cwarn2.

To know more about cwarn2 and other contributions , please visit http://syntax.cwarn23.info/PHP:_Contents . There are other great scenarios where cwarn2 has contributed with solutions.

Below is the cwarn2’s function to handle dates pre-1970:

==================================================================================
=================== cwarn2 function to handle dates pre-1970 ================================

function get_date($date,$timestamp=false) {
    if ($timestamp==false) {
        $timestamp=time();
        } else {
        $timestamp=round($timestamp);
        }
    if ($timestamp>=0) {
        return date($date,$timestamp);
        } else {
        if ($timestamp$timestamp;) { //year loop
            $year--;
            for ($k=31; $k>0;$k--) { //month loop (December)
                $month=12;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (November)
                $month=11;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (October)
                $month=10;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (September)
                $month=9;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (August)
                $month=8;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (July)
                $month=7;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (June)
                $month=6;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (May)
                $month=5;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (April)
                $month=4;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (March)
                $month=3;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            if (round((1972-$year)/4)==((1972-$year)/4)) {
                $j=29;
                } else {
                $j=28;
                }
            for ($k=$j; $k>0;$k--) { //month loop (February)
                $month=2;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (January)
                $month=1;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
 
            }
        }
        if ($hour>12) {
            $ampm='PM';
            $pmam='pm';
            } else {
            $ampm='AM';
            $pmam='am';
            }
        if ($hour>12) {
            $hr=$hour-12;
            } else {
            $hr=$hour;
            }
        if ($second<10) {
            $second='0'.$second;
            }
        if ($minute<10) {
            $minute='0'.$minute;
            }
        return str_replace(array('Y','n','j','H','h','i','s','a','A'),array($year,$month,$day,$hour,$hr,$minute,$second,$pmam,$ampm),$date);
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: