943,641 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 867
  • PHP RSS
Sep 3rd, 2009
0

Date pre-1970, strtotime and safestrtotime nor mktime is of any help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
hbmarar is offline Offline
187 posts
since Apr 2005
Sep 4th, 2009
0

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 4th, 2009
0

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

Hm, thanks for this. I had the same issue.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Wonen is offline Offline
1 posts
since Sep 2009
Sep 4th, 2009
0

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

There is an add in library for this function,
not sure how good it is,
http://phplens.com/phpeverywhere/node/view/16
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Sep 4th, 2009
0

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

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------------------------------
PHP Syntax (Toggle Plain Text)
  1. function Hsafestrtotime ($s) {
  2. $basetime = 0;
  3. if (preg_match ("/19(\d\d)/", $s, $m) && ($m[1] < 70)) {
  4. $s = preg_replace ("/19\d\d/", 1900 + $m[1]+68, $s);
  5. $basetime = 0x80000000 + 1570448;
  6. }
  7. return $basetime + strtotime ($s);
  8. }
--------------------------Function2--------------------------
PHP Syntax (Toggle Plain Text)
  1. function safestrtotime($strInput) {
  2. $iVal = -1;
  3. for ($i=1900; $i<=1969; $i++) {
  4. # Check for this year string in date
  5. $strYear = (string)$i;
  6. if (!(strpos($strInput, $strYear)===false)) {
  7. $replYear = $strYear;
  8. $yearSkew = 1970 - $i;
  9. $strInput = str_replace($strYear, "1970", $strInput);
  10. };
  11. };
  12. $iVal = strtotime($strInput);
  13. if ($yearSkew > 0) {
  14. $numSecs = (60 * 60 * 24 * 365 * $yearSkew);
  15. $iVal = $iVal - $numSecs;
  16. $numLeapYears = 0; # Work out number of leap years in period
  17. for ($j=$replYear; $j<=1969; $j++) {
  18. $thisYear = $j;
  19. $isLeapYear = false;
  20. # Is div by 4?
  21. if (($thisYear % 4) == 0) {
  22. $isLeapYear = true;
  23. };
  24. # Is div by 100?
  25. if (($thisYear % 100) == 0) {
  26. $isLeapYear = false;
  27. };
  28. # Is div by 1000?
  29. if (($thisYear % 1000) == 0) {
  30. $isLeapYear = true;
  31. };
  32. if ($isLeapYear == true) {
  33. $numLeapYears++;
  34. };
  35. };
  36. $iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
  37. };
  38. return($iVal);
  39. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
hbmarar is offline Offline
187 posts
since Apr 2005
Sep 4th, 2009
0

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

Click to Expand / Collapse  Quote originally posted by almostbob ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
hbmarar is offline Offline
187 posts
since Apr 2005
Sep 4th, 2009
1

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

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)
  1. <?
  2. function get_date($date,$timestamp=false) {
  3. if ($timestamp==false) {
  4. $timestamp=time();
  5. } else {
  6. $timestamp=round($timestamp);
  7. }
  8. if ($timestamp>=0) {
  9. return date($date,$timestamp);
  10. } else {
  11. if ($timestamp<-100000000) { die('This timestamp will take too long to calculate'); }
  12. //calculate negative time
  13. $year=1970;
  14. for ($i=-1;$i>$timestamp;) { //year loop
  15. $year--;
  16. for ($k=31; $k>0;$k--) { //month loop (December)
  17. $month=12;
  18. $day=$k;
  19. for ($l=24; $l>0;$l--) {
  20. $hour=$l;
  21. for ($m=59; $m>-1;$m--) {
  22. $minute=$m;
  23. for ($n=59; $n>-1;$n--, $i--) {
  24. $second=$n;
  25. if ($i==$timestamp) { break;break;break;break;break; }
  26. }
  27. if ($i==$timestamp) { break;break;break;break; }
  28. }
  29. if ($i==$timestamp) { break;break;break; }
  30. }
  31. if ($i==$timestamp) { break;break; }
  32. }
  33. if ($i==$timestamp) { break; }
  34. for ($k=30; $k>0;$k--) { //month loop (November)
  35. $month=11;
  36. $day=$k;
  37. for ($l=24; $l>0;$l--) {
  38. $hour=$l;
  39. for ($m=59; $m>-1;$m--) {
  40. $minute=$m;
  41. for ($n=59; $n>-1;$n--, $i--) {
  42. $second=$n;
  43. if ($i==$timestamp) { break;break;break;break;break; }
  44. }
  45. if ($i==$timestamp) { break;break;break;break; }
  46. }
  47. if ($i==$timestamp) { break;break;break; }
  48. }
  49. if ($i==$timestamp) { break;break; }
  50. }
  51. if ($i==$timestamp) { break; }
  52. for ($k=31; $k>0;$k--) { //month loop (October)
  53. $month=10;
  54. $day=$k;
  55. for ($l=24; $l>0;$l--) {
  56. $hour=$l;
  57. for ($m=59; $m>-1;$m--) {
  58. $minute=$m;
  59. for ($n=59; $n>-1;$n--, $i--) {
  60. $second=$n;
  61. if ($i==$timestamp) { break;break;break;break;break; }
  62. }
  63. if ($i==$timestamp) { break;break;break;break; }
  64. }
  65. if ($i==$timestamp) { break;break;break; }
  66. }
  67. if ($i==$timestamp) { break;break; }
  68. }
  69. if ($i==$timestamp) { break; }
  70. for ($k=30; $k>0;$k--) { //month loop (September)
  71. $month=9;
  72. $day=$k;
  73. for ($l=24; $l>0;$l--) {
  74. $hour=$l;
  75. for ($m=59; $m>-1;$m--) {
  76. $minute=$m;
  77. for ($n=59; $n>-1;$n--, $i--) {
  78. $second=$n;
  79. if ($i==$timestamp) { break;break;break;break;break; }
  80. }
  81. if ($i==$timestamp) { break;break;break;break; }
  82. }
  83. if ($i==$timestamp) { break;break;break; }
  84. }
  85. if ($i==$timestamp) { break;break; }
  86. }
  87. if ($i==$timestamp) { break; }
  88. for ($k=31; $k>0;$k--) { //month loop (August)
  89. $month=8;
  90. $day=$k;
  91. for ($l=24; $l>0;$l--) {
  92. $hour=$l;
  93. for ($m=59; $m>-1;$m--) {
  94. $minute=$m;
  95. for ($n=59; $n>-1;$n--, $i--) {
  96. $second=$n;
  97. if ($i==$timestamp) { break;break;break;break;break; }
  98. }
  99. if ($i==$timestamp) { break;break;break;break; }
  100. }
  101. if ($i==$timestamp) { break;break;break; }
  102. }
  103. if ($i==$timestamp) { break;break; }
  104. }
  105. if ($i==$timestamp) { break; }
  106. for ($k=31; $k>0;$k--) { //month loop (July)
  107. $month=7;
  108. $day=$k;
  109. for ($l=24; $l>0;$l--) {
  110. $hour=$l;
  111. for ($m=59; $m>-1;$m--) {
  112. $minute=$m;
  113. for ($n=59; $n>-1;$n--, $i--) {
  114. $second=$n;
  115. if ($i==$timestamp) { break;break;break;break;break; }
  116. }
  117. if ($i==$timestamp) { break;break;break;break; }
  118. }
  119. if ($i==$timestamp) { break;break;break; }
  120. }
  121. if ($i==$timestamp) { break;break; }
  122. }
  123. if ($i==$timestamp) { break; }
  124. for ($k=30; $k>0;$k--) { //month loop (June)
  125. $month=6;
  126. $day=$k;
  127. for ($l=24; $l>0;$l--) {
  128. $hour=$l;
  129. for ($m=59; $m>-1;$m--) {
  130. $minute=$m;
  131. for ($n=59; $n>-1;$n--, $i--) {
  132. $second=$n;
  133. if ($i==$timestamp) { break;break;break;break;break; }
  134. }
  135. if ($i==$timestamp) { break;break;break;break; }
  136. }
  137. if ($i==$timestamp) { break;break;break; }
  138. }
  139. if ($i==$timestamp) { break;break; }
  140. }
  141. if ($i==$timestamp) { break; }
  142. for ($k=31; $k>0;$k--) { //month loop (May)
  143. $month=5;
  144. $day=$k;
  145. for ($l=24; $l>0;$l--) {
  146. $hour=$l;
  147. for ($m=59; $m>-1;$m--) {
  148. $minute=$m;
  149. for ($n=59; $n>-1;$n--, $i--) {
  150. $second=$n;
  151. if ($i==$timestamp) { break;break;break;break;break; }
  152. }
  153. if ($i==$timestamp) { break;break;break;break; }
  154. }
  155. if ($i==$timestamp) { break;break;break; }
  156. }
  157. if ($i==$timestamp) { break;break; }
  158. }
  159. if ($i==$timestamp) { break; }
  160. for ($k=30; $k>0;$k--) { //month loop (April)
  161. $month=4;
  162. $day=$k;
  163. for ($l=24; $l>0;$l--) {
  164. $hour=$l;
  165. for ($m=59; $m>-1;$m--) {
  166. $minute=$m;
  167. for ($n=59; $n>-1;$n--, $i--) {
  168. $second=$n;
  169. if ($i==$timestamp) { break;break;break;break;break; }
  170. }
  171. if ($i==$timestamp) { break;break;break;break; }
  172. }
  173. if ($i==$timestamp) { break;break;break; }
  174. }
  175. if ($i==$timestamp) { break;break; }
  176. }
  177. if ($i==$timestamp) { break; }
  178. for ($k=31; $k>0;$k--) { //month loop (March)
  179. $month=3;
  180. $day=$k;
  181. for ($l=24; $l>0;$l--) {
  182. $hour=$l;
  183. for ($m=59; $m>-1;$m--) {
  184. $minute=$m;
  185. for ($n=59; $n>-1;$n--, $i--) {
  186. $second=$n;
  187. if ($i==$timestamp) { break;break;break;break;break; }
  188. }
  189. if ($i==$timestamp) { break;break;break;break; }
  190. }
  191. if ($i==$timestamp) { break;break;break; }
  192. }
  193. if ($i==$timestamp) { break;break; }
  194. }
  195. if ($i==$timestamp) { break; }
  196. if (round((1972-$year)/4)==((1972-$year)/4)) {
  197. $j=29;
  198. } else {
  199. $j=28;
  200. }
  201. for ($k=$j; $k>0;$k--) { //month loop (February)
  202. $month=2;
  203. $day=$k;
  204. for ($l=24; $l>0;$l--) {
  205. $hour=$l;
  206. for ($m=59; $m>-1;$m--) {
  207. $minute=$m;
  208. for ($n=59; $n>-1;$n--, $i--) {
  209. $second=$n;
  210. if ($i==$timestamp) { break;break;break;break;break; }
  211. }
  212. if ($i==$timestamp) { break;break;break;break; }
  213. }
  214. if ($i==$timestamp) { break;break;break; }
  215. }
  216. if ($i==$timestamp) { break;break; }
  217. }
  218. if ($i==$timestamp) { break; }
  219. for ($k=31; $k>0;$k--) { //month loop (January)
  220. $month=1;
  221. $day=$k;
  222. for ($l=24; $l>0;$l--) {
  223. $hour=$l;
  224. for ($m=59; $m>-1;$m--) {
  225. $minute=$m;
  226. for ($n=59; $n>-1;$n--, $i--) {
  227. $second=$n;
  228. if ($i==$timestamp) { break;break;break;break;break; }
  229. }
  230. if ($i==$timestamp) { break;break;break;break; }
  231. }
  232. if ($i==$timestamp) { break;break;break; }
  233. }
  234. if ($i==$timestamp) { break;break; }
  235. }
  236. if ($i==$timestamp) { break; }
  237.  
  238. }
  239. }
  240. if ($hour>12) {
  241. $ampm='AM';
  242. $pmam='am';
  243. } else {
  244. $ampm='PM';
  245. $pmam='pm';
  246. }
  247. if ($hour>12) {
  248. $hr=$hour-12;
  249. } else {
  250. $hr=$hour;
  251. }
  252. if ($second<10) {
  253. $second='0'.$second;
  254. }
  255. if ($minute<10) {
  256. $minute='0'.$minute;
  257. }
  258. return str_replace(array('Y','n','j','H','h','i','s','a','A'),array($year,$month,$day,$hour,$hr,$minute,$second,$pmam,$ampm),$date);
  259. }
  260. echo get_date('Y-n-j.....h:i:s A',-10000000);
  261. ?>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 5th, 2009
0

Re: Date pre-1970, strtotime and safestrtotime nor mktime is of any help

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)
  1. function get_date($date,$timestamp=false) {
  2. if ($timestamp==false) {
  3. $timestamp=time();
  4. } else {
  5. $timestamp=round($timestamp);
  6. }
  7. if ($timestamp>=0) {
  8. return date($date,$timestamp);
  9. } else {
  10. if ($timestamp<-62168515200) { die('This timestamp was during before the year zero there for will not be calculated'); }
  11. //calculate negative time
  12. $year=1970;
  13. for ($i=-1;$i>$timestamp;) { //year loop
  14. $year--;
  15. for ($k=31; $k>0;$k--) { //month loop (December)
  16. $month=12;
  17. $day=$k;
  18. if (($i-86400)>$timestamp) {
  19. $i-=86400;
  20. $l=24; $hour=24;
  21. $m=59; $minute=59;
  22. $n=59; $second=59;
  23. } else {
  24. for ($l=24; $l>0;$l--) {
  25. $hour=$l;
  26. for ($m=59; $m>-1;$m--) {
  27. $minute=$m;
  28. for ($n=59; $n>-1;$n--, $i--) {
  29. $second=$n;
  30. if ($i==$timestamp) { break;break;break;break;break; }
  31. }
  32. if ($i==$timestamp) { break;break;break;break; }
  33. }
  34. if ($i==$timestamp) { break;break;break; }
  35. }
  36. }
  37. if ($i==$timestamp) { break;break; }
  38. }
  39. if ($i==$timestamp) { break; }
  40. for ($k=30; $k>0;$k--) { //month loop (November)
  41. $month=11;
  42. $day=$k;
  43. if (($i-86400)>$timestamp) {
  44. $i-=86400;
  45. $l=24; $hour=24;
  46. $m=59; $minute=59;
  47. $n=59; $second=59;
  48. } else {
  49. for ($l=24; $l>0;$l--) {
  50. $hour=$l;
  51. for ($m=59; $m>-1;$m--) {
  52. $minute=$m;
  53. for ($n=59; $n>-1;$n--, $i--) {
  54. $second=$n;
  55. if ($i==$timestamp) { break;break;break;break;break; }
  56. }
  57. if ($i==$timestamp) { break;break;break;break; }
  58. }
  59. if ($i==$timestamp) { break;break;break; }
  60. }
  61. }
  62. if ($i==$timestamp) { break;break; }
  63. }
  64. if ($i==$timestamp) { break; }
  65. for ($k=31; $k>0;$k--) { //month loop (October)
  66. $month=10;
  67. $day=$k;
  68. if (($i-86400)>$timestamp) {
  69. $i-=86400;
  70. $l=24; $hour=24;
  71. $m=59; $minute=59;
  72. $n=59; $second=59;
  73. } else {
  74. for ($l=24; $l>0;$l--) {
  75. $hour=$l;
  76. for ($m=59; $m>-1;$m--) {
  77. $minute=$m;
  78. for ($n=59; $n>-1;$n--, $i--) {
  79. $second=$n;
  80. if ($i==$timestamp) { break;break;break;break;break; }
  81. }
  82. if ($i==$timestamp) { break;break;break;break; }
  83. }
  84. if ($i==$timestamp) { break;break;break; }
  85. }
  86. }
  87. if ($i==$timestamp) { break;break; }
  88. }
  89. if ($i==$timestamp) { break; }
  90. for ($k=30; $k>0;$k--) { //month loop (September)
  91. $month=9;
  92. $day=$k;
  93. if (($i-86400)>$timestamp) {
  94. $i-=86400;
  95. $l=24; $hour=24;
  96. $m=59; $minute=59;
  97. $n=59; $second=59;
  98. } else {
  99. for ($l=24; $l>0;$l--) {
  100. $hour=$l;
  101. for ($m=59; $m>-1;$m--) {
  102. $minute=$m;
  103. for ($n=59; $n>-1;$n--, $i--) {
  104. $second=$n;
  105. if ($i==$timestamp) { break;break;break;break;break; }
  106. }
  107. if ($i==$timestamp) { break;break;break;break; }
  108. }
  109. if ($i==$timestamp) { break;break;break; }
  110. }
  111. }
  112. if ($i==$timestamp) { break;break; }
  113. }
  114. if ($i==$timestamp) { break; }
  115. for ($k=31; $k>0;$k--) { //month loop (August)
  116. $month=8;
  117. $day=$k;
  118. if (($i-86400)>$timestamp) {
  119. $i-=86400;
  120. $l=24; $hour=24;
  121. $m=59; $minute=59;
  122. $n=59; $second=59;
  123. } else {
  124. for ($l=24; $l>0;$l--) {
  125. $hour=$l;
  126. for ($m=59; $m>-1;$m--) {
  127. $minute=$m;
  128. for ($n=59; $n>-1;$n--, $i--) {
  129. $second=$n;
  130. if ($i==$timestamp) { break;break;break;break;break; }
  131. }
  132. if ($i==$timestamp) { break;break;break;break; }
  133. }
  134. if ($i==$timestamp) { break;break;break; }
  135. }
  136. }
  137. if ($i==$timestamp) { break;break; }
  138. }
  139. if ($i==$timestamp) { break; }
  140. for ($k=31; $k>0;$k--) { //month loop (July)
  141. $month=7;
  142. $day=$k;
  143. if (($i-86400)>$timestamp) {
  144. $i-=86400;
  145. $l=24; $hour=24;
  146. $m=59; $minute=59;
  147. $n=59; $second=59;
  148. } else {
  149. for ($l=24; $l>0;$l--) {
  150. $hour=$l;
  151. for ($m=59; $m>-1;$m--) {
  152. $minute=$m;
  153. for ($n=59; $n>-1;$n--, $i--) {
  154. $second=$n;
  155. if ($i==$timestamp) { break;break;break;break;break; }
  156. }
  157. if ($i==$timestamp) { break;break;break;break; }
  158. }
  159. if ($i==$timestamp) { break;break;break; }
  160. }
  161. }
  162. if ($i==$timestamp) { break;break; }
  163. }
  164. if ($i==$timestamp) { break; }
  165. for ($k=30; $k>0;$k--) { //month loop (June)
  166. $month=6;
  167. $day=$k;
  168. if (($i-86400)>$timestamp) {
  169. $i-=86400;
  170. $l=24; $hour=24;
  171. $m=59; $minute=59;
  172. $n=59; $second=59;
  173. } else {
  174. for ($l=24; $l>0;$l--) {
  175. $hour=$l;
  176. for ($m=59; $m>-1;$m--) {
  177. $minute=$m;
  178. for ($n=59; $n>-1;$n--, $i--) {
  179. $second=$n;
  180. if ($i==$timestamp) { break;break;break;break;break; }
  181. }
  182. if ($i==$timestamp) { break;break;break;break; }
  183. }
  184. if ($i==$timestamp) { break;break;break; }
  185. }
  186. }
  187. if ($i==$timestamp) { break;break; }
  188. }
  189. if ($i==$timestamp) { break; }
  190. for ($k=31; $k>0;$k--) { //month loop (May)
  191. $month=5;
  192. $day=$k;
  193. if (($i-86400)>$timestamp) {
  194. $i-=86400;
  195. $l=24; $hour=24;
  196. $m=59; $minute=59;
  197. $n=59; $second=59;
  198. } else {
  199. for ($l=24; $l>0;$l--) {
  200. $hour=$l;
  201. for ($m=59; $m>-1;$m--) {
  202. $minute=$m;
  203. for ($n=59; $n>-1;$n--, $i--) {
  204. $second=$n;
  205. if ($i==$timestamp) { break;break;break;break;break; }
  206. }
  207. if ($i==$timestamp) { break;break;break;break; }
  208. }
  209. if ($i==$timestamp) { break;break;break; }
  210. }
  211. }
  212. if ($i==$timestamp) { break;break; }
  213. }
  214. if ($i==$timestamp) { break; }
  215. for ($k=30; $k>0;$k--) { //month loop (April)
  216. $month=4;
  217. $day=$k;
  218. if (($i-86400)>$timestamp) {
  219. $i-=86400;
  220. $l=24; $hour=24;
  221. $m=59; $minute=59;
  222. $n=59; $second=59;
  223. } else {
  224. for ($l=24; $l>0;$l--) {
  225. $hour=$l;
  226. for ($m=59; $m>-1;$m--) {
  227. $minute=$m;
  228. for ($n=59; $n>-1;$n--, $i--) {
  229. $second=$n;
  230. if ($i==$timestamp) { break;break;break;break;break; }
  231. }
  232. if ($i==$timestamp) { break;break;break;break; }
  233. }
  234. if ($i==$timestamp) { break;break;break; }
  235. }
  236. }
  237. if ($i==$timestamp) { break;break; }
  238. }
  239. if ($i==$timestamp) { break; }
  240. for ($k=31; $k>0;$k--) { //month loop (March)
  241. $month=3;
  242. $day=$k;
  243. if (($i-86400)>$timestamp) {
  244. $i-=86400;
  245. $l=24; $hour=24;
  246. $m=59; $minute=59;
  247. $n=59; $second=59;
  248. } else {
  249. for ($l=24; $l>0;$l--) {
  250. $hour=$l;
  251. for ($m=59; $m>-1;$m--) {
  252. $minute=$m;
  253. for ($n=59; $n>-1;$n--, $i--) {
  254. $second=$n;
  255. if ($i==$timestamp) { break;break;break;break;break; }
  256. }
  257. if ($i==$timestamp) { break;break;break;break; }
  258. }
  259. if ($i==$timestamp) { break;break;break; }
  260. }
  261. }
  262. if ($i==$timestamp) { break;break; }
  263. }
  264. if ($i==$timestamp) { break; }
  265. if (round((1972-$year)/4)==((1972-$year)/4)) {
  266. $j=29;
  267. } else {
  268. $j=28;
  269. }
  270. for ($k=$j; $k>0;$k--) { //month loop (February)
  271. $month=2;
  272. $day=$k;
  273. if (($i-86400)>$timestamp) {
  274. $i-=86400;
  275. $l=24; $hour=24;
  276. $m=59; $minute=59;
  277. $n=59; $second=59;
  278. } else {
  279. for ($l=24; $l>0;$l--) {
  280. $hour=$l;
  281. for ($m=59; $m>-1;$m--) {
  282. $minute=$m;
  283. for ($n=59; $n>-1;$n--, $i--) {
  284. $second=$n;
  285. if ($i==$timestamp) { break;break;break;break;break; }
  286. }
  287. if ($i==$timestamp) { break;break;break;break; }
  288. }
  289. if ($i==$timestamp) { break;break;break; }
  290. }
  291. }
  292. if ($i==$timestamp) { break;break; }
  293. }
  294. if ($i==$timestamp) { break; }
  295. for ($k=31; $k>0;$k--) { //month loop (January)
  296. $month=1;
  297. $day=$k;
  298. if (($i-86400)>$timestamp) {
  299. $i-=86400;
  300. $l=24; $hour=24;
  301. $m=59; $minute=59;
  302. $n=59; $second=59;
  303. } else {
  304. for ($l=24; $l>0;$l--) {
  305. $hour=$l;
  306. for ($m=59; $m>-1;$m--) {
  307. $minute=$m;
  308. for ($n=59; $n>-1;$n--, $i--) {
  309. $second=$n;
  310. if ($i==$timestamp) { break;break;break;break;break; }
  311. }
  312. if ($i==$timestamp) { break;break;break;break; }
  313. }
  314. if ($i==$timestamp) { break;break;break; }
  315. }
  316. }
  317. if ($i==$timestamp) { break;break; }
  318. }
  319. if ($i==$timestamp) { break; }
  320.  
  321. }
  322. }
  323. if ($hour>12) {
  324. $ampm='PM';
  325. $pmam='pm';
  326. } else {
  327. $ampm='AM';
  328. $pmam='am';
  329. }
  330. if ($hour>12) {
  331. $hr=$hour-12;
  332. } else {
  333. $hr=$hour;
  334. }
  335. if ($second<10) {
  336. $second='0'.$second;
  337. }
  338. if ($minute<10) {
  339. $minute='0'.$minute;
  340. }
  341. return str_replace(array('Y','n','j','H','h','i','s','a','A'),array($year,$month,$day,$hour,$hr,$minute,$second,$pmam,$ampm),$date);
  342. }
  343.  
  344.  
  345. //now to use it.
  346. echo get_date('Y-n-j.....h:i:s A',-172800);
  347. ?>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to turn my codes into an array?
Next Thread in PHP Forum Timeline: how to store image in a folder and mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC