PHP:: Pre-1970 Dates :: Functions Explored for a task
The purpose of this post is to collect different possible solutions or custom functions available on net or shared for my query in Web Development forum to solve a issue with dates before 1970.
Scenario: Today at work, I had 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.
Input date: 1/1/1900
Output desired as inserted in database : 1900-01-01
First Efforts to resolve , but didnt worked out is :-
=> Tried strtotime() and date()+mktime() but all gave date reset to 1970, Jan, 01.
- strftime(“%Y-%m-%d”, strtotime($timestamp));
- date(‘Y-M-d’, mktime(0,0,0,1,1,1900))
=> safestrtotime() :: There is safestrtotime in the strtotime() page in php.net which seems to work but the dates is not precisely the same anymore . For eg:- strftime(“%Y-%m-%d”, safestrtotime($timestamp)); gave 1901-12-14.
=> safestrtotime function was as below:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Below Function is available in strtotime function documentation page in www.php.net
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);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
=> Another function using basetime explored is as below:-
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);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function convertTime($dformat,$sformat,$ts) {
extract(strptime($ts,$sformat));
return strftime($dformat,mktime(
intval($tm_hour),
intval($tm_min),
intval($tm_sec),
intval($tm_mon)+1,
intval($tm_mday),
intval($tm_year)+1900
));
}
Loading...
Solution #1: Using mktime and then type casting
http://saffrongeek.wordpress.com/2009/09/05/php-pre-1970-dates-solution-simple-weird-one/
saffrongeek - September 15, 2009 at 7:30 am
Solution #2: cwarn2 get date function
http://saffrongeek.wordpress.com/2009/09/05/cwarn2s-classic-function-handles-dates-pre-1970/
saffrongeek - September 15, 2009 at 7:31 am