dwno Posted January 8, 2005 Posted January 8, 2005 I've tested the following code in zen-cart, but the function is exactly the same in osCommerce, so I expect it to work just as good here. Original function (both in osC and zen): function tep_date_raw($date, $reverse = false) { if ($reverse) { return substr($date, 3, 2) . substr($date, 0, 2) . substr($date, 6, 4); } else { return substr($date, 6, 4) . substr($date, 0, 2) . substr($date, 3, 2); } } (copied'n'pasted from zen-cart's forum, only difference is function name) I made some improvements to the function tep_date_raw(). You should now be able to enter your birthday in several ways, based on what the constant DATE_FORMAT is set to. For example, you can insert 270282, 27282, 2721982, 27021982, 27 lhabfdkj /&%?" 02 -82, 1182 (which becomes 01011982), 11182 (which becomes 11011982), 27.02.82, 27/02-1982, etc. Some of those will probably never be used, but the best part IMO is that you don't have to separate the date with slashes anymore. (I found it a bit annoying actually.) There should also be made changes to some javascript somewhere, so you only need to insert YY instead of YYYY, but I haven't looked at that yet. (--comment-- I'm not sure if this is relevent for osCommerce) DATE_FORMAT can be set to any combination of dmY except dYm or mYd. (Those aren't used anywhere, are they?) Casing doesn't matter, and any other characters, such as slash or dash, don't matter either. function tep_date_raw($date, $reverse = false) { $date = ereg_replace("[^0-9]","", $date); $dformat=DATE_FORMAT; $dformat = ereg_replace("[^dmy]","", $dformat); $dd="([0-2][1-9]|[1-3][0-1]|[1-9])"; $mm="(0?[1-9]|1[0-2])"; $yyyy="((19|20)?[0-9]{2})"; if (strtolower($dformat)=="dmy") {$regexp=$dd . $mm . $yyyy; $d="2"; $m="3"; $y="4";} else if (strtolower($dformat)=="mdy") {$regexp=$mm . $dd . $yyyy; $d="3"; $m="2"; $y="4";} else if (strtolower($dformat)=="ydm") {$regexp=$yyyy . $dd . $mm; $d="4"; $m="5"; $y="2";} else if (strtolower($dformat)=="ymd") {$regexp=$yyyy . $mm . $dd; $d="5"; $m="4"; $y="2";} ereg("(^" . $regexp . "$)", $date, $regs); // fix value of day if (strlen($regs[$d])=="1") {$regs[$d]="0" . $regs[$d];} // fix value of month if (strlen($regs[$m])=="1") {$regs[$m]="0" . $regs[$m];} // fix value of year if (strlen($regs[$y])=="2") {$regs[$y]="19" . $regs[$y];} if ($reverse) { $date=$regs[$d] . $regs[$m] . $regs[$y]; return $date; } else { $date=$regs[$y] . $regs[$m] . $regs[$d]; return $date; } } One thing I'm not entirely sure of; $reverse should return ddmmyyyy, and not vice-versa, right? So what do you think? And let me know if you want additional comments inside the function.
dwno Posted January 8, 2005 Author Posted January 8, 2005 Well I just noticed an unneccessary line; replace if ($reverse) { $date=$regs[$d] . $regs[$m] . $regs[$y]; return $date; } else { $date=$regs[$y] . $regs[$m] . $regs[$d]; return $date; } } with if ($reverse) { $date=$regs[$d] . $regs[$m] . $regs[$y]; } else { $date=$regs[$y] . $regs[$m] . $regs[$d]; } return $date; }
dwno Posted January 8, 2005 Author Posted January 8, 2005 Err, come to think of it, I'm not sure what those numbers in the original code are for; return substr($date, 3, 2) . substr($date, 0, 2) . substr($date, 6, 4); what does those numbers mean? I think they're useless, but I can't say for sure. Perhaps they should also be part of the new function.
dwno Posted January 8, 2005 Author Posted January 8, 2005 Sorry, I was very tired when I posted that code yesterday, and it was incorrect. It should be; function tep_date_raw($date, $reverse = false) { $date = ereg_replace("[^0-9]","", $date); $dformat = ereg_replace("[^dmy]","", strtolower(DATE_FORMAT)); $dd="([0-2][1-9]|[1-3][0-1]|[1-9])"; $mm="(0?[1-9]|1[0-2])"; $yyyy="((19|20)?[0-9]{2})"; if ($dformat=="dmy") {$regexp=$dd . $mm . $yyyy; $d="2"; $m="3"; $y="4";} else if ($dformat=="mdy") {$regexp=$mm . $dd . $yyyy; $d="3"; $m="2"; $y="4";} else if ($dformat=="ydm") {$regexp=$yyyy . $dd . $mm; $d="4"; $m="5"; $y="2";} else if ($dformat=="ymd") {$regexp=$yyyy . $mm . $dd; $d="5"; $m="4"; $y="2";} ereg("(^" . $regexp . "$)", $date, $regs); // fix value of day if (strlen($regs[$d])=="1") {$regs[$d]="0" . $regs[$d];} // fix value of month if (strlen($regs[$m])=="1") {$regs[$m]="0" . $regs[$m];} // fix value of year if (strlen($regs[$y])=="2") {$regs[$y]="19" . $regs[$y];} if ($reverse) { $date=$regs[$d] . $regs[$m] . $regs[$y]; } else { $date=$regs[$y] . $regs[$m] . $regs[$d]; } return $date; } Never mind comments about javascript, the minimum values are of course changed in the admin panel. And I've also tested it on osCommerce now :) And if you're wondering which files this function is stored in, it's includes/languages/english.php and admin/includes/languages/english.php.
freefall1 Posted January 17, 2005 Posted January 17, 2005 the numbers stand for the character position in the date line 11/11/1998 1,2 = first 11 start at position 1 en 2 chars long 3,2 = second 11 start at position 3 and 2 chars long 6,4 = 1998 start at position 6 and 4 chars long Hope this 'll do you any good, and if not.... well .... useless info is also info .... :-S
dwno Posted January 17, 2005 Author Posted January 17, 2005 :blush: oh yeah, that's a native php function, of course.. I wasn't thinking straight when posting that; I thought it was a function especially made for osCommerce. :rolleyes: Thanks anyway :thumbsup:
dwno Posted January 17, 2005 Author Posted January 17, 2005 I also changed the last "else if" to simply "else", so in case DATE_FORMAT isn't set, you'll get the date in ISO 8601 format (yyyymmdd). Here's the final (?) code: function tep_date_raw($date, $reverse = false) { $date = ereg_replace("[^0-9]","", $date); $dformat = ereg_replace("[^dmy]","", strtolower(DATE_FORMAT)); $dd="([0-2][1-9]|[1-3][0-1]|[1-9])"; $mm="(0?[1-9]|1[0-2])"; $yyyy="((19|20)?[0-9]{2})"; if ($dformat=="dmy") {$regexp=$dd . $mm . $yyyy; $d="2"; $m="3"; $y="4";} else if ($dformat=="mdy") {$regexp=$mm . $dd . $yyyy; $d="3"; $m="2"; $y="4";} else if ($dformat=="ydm") {$regexp=$yyyy . $dd . $mm; $d="4"; $m="5"; $y="2";} else {$regexp=$yyyy . $mm . $dd; $d="5"; $m="4"; $y="2";} ereg("(^" . $regexp . "$)", $date, $regs); // fix value of day if (strlen($regs[$d])=="1") {$regs[$d]="0" . $regs[$d];} // fix value of month if (strlen($regs[$m])=="1") {$regs[$m]="0" . $regs[$m];} // fix value of year if (strlen($regs[$y])=="2") {$regs[$y]="19" . $regs[$y];} if ($reverse) { $date=$regs[$d] . $regs[$m] . $regs[$y]; } else { $date=$regs[$y] . $regs[$m] . $regs[$d]; } return $date; } Like I said on the Zen-cart forum; I can't see a reason for not changing the core function into this, because with the old function new customers needed to insert DOB like this: mm/dd/yyyy, and with those slashes. This new one, takes whatever the constant DATE_FORMAT is set to in the language files.
dwno Posted January 28, 2005 Author Posted January 28, 2005 I don't know if anyone are interested in this, but I made some more improvements to this function. The new additions are more fancy than useful, but if anyone want it, just go ahead and try it, otherwise ignore it. THIS HAS NOT YET BEEN TESTED ON osCommerce, but it works perfectly in zen-cart and the functions are entirely the same (except the name), and I did test the previous codes in osC, so I can't really see a reason why this should not work with osC. (As far as I can see, this code should work on any project not even related to osC.) This function allows you to enter your birth date like "10th of january 1970", or simply "10jan70". This makes little room for user errors. (It's sorta like internet explorer, it guesses what you want :lol: ) What to do: 1. Add the following line somewhere. Perhaps in includes/languages/<language>.php. define('MONTH_ABBR', 'jan feb mar apr may jun jul aug sep oct nov dec'); 2. (optional) Edit the following line, also in includes/languages/<language>.php, to whatever you like (as long as d, m and y is a part of it.) define('DATE_FORMAT', 'd/m-Y'); 3. Replace the old function tep_date_raw (still includes/languages/<language>.php) function tep_date_raw($date, $reverse = false) { if ($reverse) { return substr($date, 3, 2) . substr($date, 0, 2) . substr($date, 6, 4); } else { return substr($date, 6, 4) . substr($date, 0, 2) . substr($date, 3, 2); } } with this one: function tep_date_raw($date, $reverse = false) { $date = strtolower($date); $month_abbr = explode(" ", " " . MONTH_ABBR); for ($i=1; $i < count($month_abbr); $i++) {$month_strpos[$i]=strpos($date, $month_abbr[$i]); if (is_int($month_strpos[$i]) && !isset($first_case)) {$first_case=$month_strpos[$i]; $m=$i;}; if (is_int($month_strpos[$i]) && $month_strpos[$i] < $first_case) {$first_case=$month_strpos[$i]; $m=$i;};}; if (isset($m) && isset($first_case)) {$date = substr_replace($date, $m, $first_case, 0);}; $date = ereg_replace("[^0-9]","", $date); $dformat = ereg_replace("[^dmy]","", strtolower(DATE_FORMAT)); $dd="([0-2][1-9]|[1-3][0-1]|[1-9])"; if (isset($m) && isset($first_case)) {$mm="(" . $m . ")";} else {$mm="(0?[1-9]|1[0-2])";}; $yyyy="((19|20)?[0-9]{2})"; if ($dformat=="dmy") {$regexp=$dd . $mm . $yyyy; $d="2"; $m="3"; $y="4";} else if ($dformat=="mdy") {$regexp=$mm . $dd . $yyyy; $d="3"; $m="2"; $y="4";} else if ($dformat=="ydm") {$regexp=$yyyy . $dd . $mm; $d="4"; $m="5"; $y="2";} else {$regexp=$yyyy . $mm . $dd; $d="5"; $m="4"; $y="2";} if (!ereg("(^" . $regexp . "$)", $date, $regs)) {return false;}; if (strlen($regs[$d])=="1") {$regs[$d]="0" . $regs[$d];} if (strlen($regs[$m])=="1") {$regs[$m]="0" . $regs[$m];} /* If year is less than or equal to the last 2 numbers of the current year, set century to 20. */ if (strlen($regs[$y])=="2") {if ($regs[$y] <= (date(y))) {$regs[$y]="20" . $regs[$y];} else {$regs[$y]="19" . $regs[$y];}} if ($regs[$m]=="01" || $regs[$m]=="03" || $regs[$m]=="05" || $regs[$m]=="07" || $regs[$m]=="08" || $regs[$m]=="10" || $regs[$m]=="12") {$no_of_days = "31";} else if ($regs[$m]=="04" || $regs[$m]=="06" || $regs[$m]=="09" || $regs[$m]=="11") {$no_of_days = "30";} else if (($regs[$y] % 4) == 0) {$no_of_days = "29";} else {$no_of_days = "28";}; if ($regs[$d] > $no_of_days) {return false;}; if ($regs[$y] > date(Y)+1) {return false;}; if ($reverse) { $date=$regs[$d] . $regs[$m] . $regs[$y]; } else { $date=$regs[$y] . $regs[$m] . $regs[$d]; } return $date; } 4. (optional) Edit admin-> configuration-> minimum values-> date of birth to 4. Examples: (when DATE_FORMAT is set to 'dmy', and the current year is 2005) 1111 -> 19110101 2180 -> 19800102 1011 -> false (there is no 0 month) 11106 -> 19060111 10105 -> 20050110 1011980 -> 19800110 30/04-1904 -> 19040430 31/04-1904 -> false (april doesn't have 31 days) 1dec70 -> 19701201 29feb2000 -> 20000229 29feb2001 -> false (not leap year) 10feb january mar 75 -> 19750210 30january feb march 02 -> 20020130 abc1feb.foo-ary/\82xyz -> 19820201 Don't worry about the examples being "day, month, year", it depends on what DATE_FORMAT is set to in includes/languages/<language>.php. For more info, see the thread on zen-cart's forum. Enjoy B) [edit: changed zen_ to tep_]
Recommended Posts
Archived
This topic is now archived and is closed to further replies.