| | |
Date pre-1970, strtotime and safestrtotime nor mktime is of any help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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
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
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.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
There is an add in library for this function,
not sure how good it is,
http://phplens.com/phpeverywhere/node/view/16
not sure how good it is,
http://phplens.com/phpeverywhere/node/view/16
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
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------------------------------
--------------------------Function2--------------------------
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------------------------------
PHP Syntax (Toggle Plain Text)
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); }
PHP Syntax (Toggle Plain Text)
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
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.
php Syntax (Toggle Plain Text)
<? 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); ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Just for anybody reading this thread in the future, I have managed to complete my script with better reliability and is as follows:
php Syntax (Toggle Plain Text)
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); ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
![]() |
Similar Threads
- Convert String to Date (PHP)
- showing date,today,yesterday and ...... (PHP)
- left side menu (PHP)
- page loading problem (PHP)
- selecting the day from the DATE field so that it includes a zero... (MySQL)
- Always return the same date(s) (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- Warning: mysql_num_rows(): (PHP)
Other Threads in the PHP Forum
- Previous Thread: How to turn my codes into an array?
- Next Thread: how to store image in a folder and mysql
| Thread Tools | Search this Thread |
# 5.2.10 access alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






