Hi ,

I have a scenario where I have to fetch the dates in 1/1/1900 format and insert it to a new database as 1900-01-01. The issue i face is my date before 1970 gets reset to 01-01-1970.

I tried as strftime("%Y-%m-%d", strtotime(''1-1-1900)).
I also tried with mktime but same.

There is a safestrtotime() in the strtotime function page in php.net but that gives me for date 01-01-1900 the date as 14-12-1901.

PHP 5.1+
OS : Ubuntu

Thanks in advance for the pointers and suggestions, I remain

With regards,

Harish

Recommended Answers

I have experimented with your problem and this appears to be a minor php bug. It seems to not handle negetive numbers properly. So to be able to do this job, you will need to make custom functions with loops that calculate the appropriate date. I will see what I …

Jump to Post

In case you want to make a custom function, the following code will do the job but is really slow. But this will give you something to play with.

<?
function get_date($date,$timestamp=false) {
    if ($timestamp==false) {
        $timestamp=time();
        } else {
        $timestamp=round($timestamp);
        }
    if ($timestamp>=0) {
        return date($date,$timestamp);
        } else { …
Jump to Post

All 7 Replies

I have experimented with your problem and this appears to be a minor php bug. It seems to not handle negetive numbers properly. So to be able to do this job, you will need to make custom functions with loops that calculate the appropriate date. I will see what I can do as it won't be an easy task (due to leap years) but good luck.

Hm, thanks for this. I had the same issue.

Function I tried other than builtin strtotime and date+ mktime is as below section but nothing gave me the exact date. Since I was getting no where, just wondered how about type cast. It seems WORKING, in the sense, I am able to put in the date as needed and precise 1900-01-01.

Input date: 1/1/1900
Output desired as inserted in database : 1900-01-01

Step #1. Exploded the input date to a array.
Step #2. Formed a needed format variable($timestamp) from the array
Step #3. Just to make sure cast as 'String' : ( String ) $timestamp

Inserted to the datebase , and the date type field holds the date correctly and the way expected.
[Note] Dont know whether above approach is correct but by posting here I believe, I could get some suggestions.

========================================================

-------------------------Function1------------------------------

function Hsafestrtotime ($s) {
	  $basetime = 0;
	  if (preg_match ("/19(\d\d)/", $s, $m) && ($m[1] < 70)) {
	    $s = preg_replace ("/19\d\d/", 1900 + $m[1]+68, $s);
	    $basetime = 0x80000000 + 1570448;
	  }
	  return $basetime + strtotime ($s);
	}

--------------------------Function2--------------------------

function safestrtotime($strInput) {
        $iVal = -1;
        for ($i=1900; $i<=1969; $i++) {
            # Check for this year string in date
            $strYear = (string)$i;
            if (!(strpos($strInput, $strYear)===false)) {
                $replYear = $strYear;
                $yearSkew = 1970 - $i;
                $strInput = str_replace($strYear, "1970", $strInput);
            };
        };
        $iVal = strtotime($strInput);
        if ($yearSkew > 0) {
            $numSecs = (60 * 60 * 24 * 365 * $yearSkew);
            $iVal = $iVal - $numSecs;
            $numLeapYears = 0;        # Work out number of leap years in period
            for ($j=$replYear; $j<=1969; $j++) {
                $thisYear = $j;
                $isLeapYear = false;
                # Is div by 4?
                if (($thisYear % 4) == 0) {
                    $isLeapYear = true;
                };
                # Is div by 100?
                if (($thisYear % 100) == 0) {
                    $isLeapYear = false;
                };
                # Is div by 1000?
                if (($thisYear % 1000) == 0) {
                    $isLeapYear = true;
                };
                if ($isLeapYear == true) {
                    $numLeapYears++;
                };
            };
            $iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
        };
        return($iVal);
    }

There is an add in library for this function,
not sure how good it is,
http://phplens.com/phpeverywhere/node/view/16

Thanks almostbob for the refernce link and knowledge. Sure, I would try it out. For now, I managed it some how but would like to try out something that is the right approach. Thanks once again.

In case you want to make a custom function, the following code will do the job but is really slow. But this will give you something to play with.

<?
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<-100000000)  { die('This timestamp will take too long to calculate'); }
        //calculate negative time
        $year=1970;
        for ($i=-1;$i>$timestamp;) { //year loop
            $year--;
            for ($k=31; $k>0;$k--) { //month loop (December)
                $month=12;
                $day=$k;
                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;
                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;
                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;
                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;
                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;
                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;
                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;
                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;
                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;
                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;
                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;
                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='AM';
            $pmam='am';
            } else {
            $ampm='PM';
            $pmam='pm';
            }
        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);
    }
echo get_date('Y-n-j.....h:i:s A',-10000000);
?>
commented: Thanks to cwarn for helping me +5

Just for anybody reading this thread in the future, I have managed to complete my script with better reliability and is as follows:

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<-62168515200)  { die('This timestamp was during before the year zero there for will not be calculated'); }
        //calculate negative time
        $year=1970;
        for ($i=-1;$i>$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);
    }


//now to use it.
echo get_date('Y-n-j.....h:i:s A',-172800);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.