Zen Cart Logo
Forums / Zen Cart Code Suggestions / Function Zen_date_raw()

Function Zen_date_raw()

Locked

Views: 2,118

Results 1 to 17 of 17
This thread is locked. New replies are disabled.
8 Jan 2005, 3:07 AM
#1
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Function Zen_date_raw()

Hi,

I made some improvements to the function zen_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.

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.

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 zen_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.

8 Jan 2005, 3:55 AM
#2
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Hmm, noticed some unneccessary code;

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;
}

8 Jan 2005, 1:35 PM
#3
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Sorry, I was dead tired when I posted that first code, and it's not correct. It should rather be;

function zen_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 my earlier comment about changing javascript, but you may want to change minimum value for date of birth in the admin panel.

14 Jan 2005, 5:32 PM
#4
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Minor adjustment of "else" case.

Looking over the code one more time I see that it won't work if DATE_FORMAT isn't set correctly. This shouldn't be a big problem, but just in case someone messes up DATE_FORMAT while translating Zen-Cart...
Just change the last "else if" to just "else", so it follows ISO 8601 (yyyymmdd) if DATE_FORMAT is not of valid syntax.

Final (?) code:

function zen_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;
}

Can I get some response on whether or not this will be included in the core?
I really can't see a reason for not to do so, because the old function didn't take DATE_FORMAT into consideration when asking for DOB under customer registration or account update. It would always require the format mm/dd/yyyy, and with slashes. Also, the old function would allow DOB's from any century :D This one allows only 19** or 20**.

-Eivind

17 Jan 2005, 8:14 AM
#5
piglet avatar

piglet

New Zenner

Join Date:
Feb 2004
Posts:
10
Plugin Contributions:
0

Re: Function Zen_date_raw()

dwno:

I really can't see a reason for not to do so, because the old function didn't take DATE_FORMAT into consideration when asking for DOB under customer registration or account update. It would always require the format mm/dd/yyyy, and with slashes.
I agree. I had to customize the customer registration page, and I suppose everyone outside of the US does the same.

24 Jan 2005, 9:29 PM
#6
chillywilly avatar

chillywilly

New Zenner

Join Date:
Jan 2005
Posts:
37
Plugin Contributions:
0

Re: Function Zen_date_raw()

Ok dumb question where does this go and what code should I be replacing?

25 Jan 2005, 12:16 AM
#7
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Sorry, my bad.

The function is inside includes/languages/<language>.php

Replace this entire section:

function zen_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);
 }
}
25 Jan 2005, 1:15 AM
#8
chillywilly avatar

chillywilly

New Zenner

Join Date:
Jan 2005
Posts:
37
Plugin Contributions:
0

Re: Function Zen_date_raw()

THank you now if only it put those darn little /////// all by its self :D Works great thank you

25 Jan 2005, 1:50 AM
#9
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

** THank you now if only it put those darn little /////// all by its self**
What do you mean? Do you still have to insert those slashes? Probably because the minimum length of the date is set to 10 in the admin. Try to set it to 8, or 5 even (when DATE_FORMAT is set to dmy, 11180 becomes 11011980, etc.)

Somewhere between 5-8, whatever fits you. :)

28 Jan 2005, 7:00 PM
#10
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

(You may disregard my earlier posted codes for this function if you want)

This is just another go at the dates issue, nothing revolutionary, but some functionality you probably never knew you wanted :lol:

Some improvements.
You can now insert month as the month's name, like "10th of january1970" or "10jan70".
There are also now better restrictions on day of month, so you can not insert "300270" anymore (february never has 30 days).
And some improvements to how century is calculated, if it is not entered.

<long_description rel=additional>
While messing around with the advanced search, I found it didn't work properly when you insert from- and to-dates. First I thought it could be something somehow related to my function zen_date_raw, since it was the date-thing that failed. So I started by looking at the function zen_checkdate, which is used by the advanced search. The first thing I thought was that the function was really messy, but it also had some nice functionality; it tries to convert abbreviated month names into the appropriate number. This means you could enter for instance "12feb2005" and it should return "20050212" (except the original function would need slashes as separators). So I took that idea and remade it so it depends on another constant. This way, you can have abbreviated month names for each language!
</long_description rel=additional>

The advanced search failure was a bug (not related to this function), and it was
solved by McAjvar (is that pronounced "MacGyver"? :D):
The 2 variables $dfrom_to_check and $dto_to_check should be changed to $dfrom and $dto. You can find them in the file
includes/blocks/blk_advanced_search_result.php
on line 138 and 142.

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');
  1. (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');

Replace the old function zen_date_raw (still includes/languages/<language>.php)

function zen_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 zen_date_raw($date, $reverse = false) {
$date = strtolower($date);
/* The second " " adds a value before MONTH_ABBR, so the first string in constant MONTH_ABBR starts at $month_abbr[1], and not $month_abbr[0] */
$month_abbr = explode(" ", " " . MONTH_ABBR);

/* In case there are more than 1 string in $month_abbr that matches $date, take the first one.
But in case one of the strings in $month_abbr should coincidentially be a part of the
non-abbreviated month name of another string in $month_abbr, we can't just take the first occurance
of the first matching string.
This is for allowing people to enter the full name of the month, like "January". Now say that in some language,
barxyz means january, while foobar means february. If the inserted month was foobar, then the returned month
would be january (because the loop starts with january and finds bar in the string, and bar is an abbreviation for barxyz (january)), while we really wanted february (foobar).
We could do it simple and depend on separators to separate day, month and year, but we don't want that. */
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);};

/* Now, as we've converted any eventual month strings into a number, remove non-numeric characters. */
$date = ereg_replace("[^0-9]","", $date);
/* For the date format, remove all characters except d, m and y. */
$dformat = ereg_replace("[^dmy]","", strtolower(DATE_FORMAT));

/* Define regex for day, month and year. */
$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})";
/* Look at the set date format, and create the entire regex line as well as set the location where day, month and year is to be found for later reference. */
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";}
/* Do the magic or return false. */
if (!ereg("(^" . $regexp . "$)", $date, $regs)) {return false;};

/* If some values are too short, fix them. */
/* 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 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];}}

/* Find maximum number of days in the current month, and make sure the day inserted is not higher. */
/* Is month one of january, march, may, july, august, october or december? */
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";}
/* Is month one of april, june, september or november? */
else if ($regs[$m]=="04" || $regs[$m]=="06" || $regs[$m]=="09" || $regs[$m]=="11") {$no_of_days = "30";}
/* So the month is february, but is it a leap year? */
else if (($regs[$y] % 4) == 0) {$no_of_days = "29";}
else {$no_of_days = "28";};
/* If the day is higher than what is allowed, it's obviously not a correct date. */
if ($regs[$d] > $no_of_days) {return false;};
/* Allow dates only into the next year. (Think new year's eve and different time-zones and even possibly wrong local time set.) */
if ($regs[$y] > date(Y)+1) {return false;};

/* Return date like ddmmyyyy (reverse) or the standard yyyymmdd? */
if ($reverse) {
$date=$regs[$d] . $regs[$m] . $regs[$y];
 } else {
$date=$regs[$y] . $regs[$m] . $regs[$d];
 }
return $date;
}

OR if you're not interested in those comments, you may remove them if you want. Wait, let me do it for you:

function zen_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;
}
  1. (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.

Enjoy B)

28 Jan 2005, 7:10 PM
#11
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

:blink: Err, let me say it for you: "ouch"!
That post was just a bit long wasn't it? It didn't look so bad in a text editor that fills the screen at 1100*1000px (or so)..

29 Jan 2005, 12:15 AM
#12
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Earlier I mentioned function zen_checkdate, which is used by the advanced search form. (and at the moment, only the advanced search form)

With minor adjustments of my function zen_date_raw, I could replace the function zen_checkdate as well.

edit includes/modules/pages/advanced_search_result/header_php.php,

replace line 61

if (!zen_checkdate($dfrom, DOB_FORMAT_STRING, $dfrom_array)) {

with

if (!zen_checkdate($dfrom, $dfrom_string)) {

and replace line 70

if (!zen_checkdate($dto, DOB_FORMAT_STRING, $dto_array)) {

with

if (!zen_checkdate($dto, $dto_string)) {

then you may replace line 79

if (mktime(0, 0, 0, $dfrom_array[1], $dfrom_array[2], $dfrom_array[0]) > mktime(0, 0, 0, $dto_array[1], $dto_array[2], $dto_array[0])) {

with

if ($dfrom_string > $dto_string) {

edit includes/functions/functions_general.php (line 403-491)

replace

 function zen_checkdate($date_to_check, $format_string, &$date_array) {
  $separator_idx = -1;

  $separators = array('-', ' ', '/', '.');
  $month_abbr = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
  $no_of_days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  $format_string = strtolower($format_string);

  if (strlen($date_to_check) != strlen($format_string)) {
   return false;
  }

  $size = sizeof($separators);
  for ($i=0; $i<$size; $i++) {
   $pos_separator = strpos($date_to_check, $separators[$i]);
   if ($pos_separator != false) {
    $date_separator_idx = $i;
    break;
   }
  }

  for ($i=0; $i<$size; $i++) {
   $pos_separator = strpos($format_string, $separators[$i]);
   if ($pos_separator != false) {
    $format_separator_idx = $i;
    break;
   }
  }

  if ($date_separator_idx != $format_separator_idx) {
   return false;
  }

  if ($date_separator_idx != -1) {
   $format_string_array = explode( $separators[$date_separator_idx], $format_string );
   if (sizeof($format_string_array) != 3) {
    return false;
   }

   $date_to_check_array = explode( $separators[$date_separator_idx], $date_to_check );
   if (sizeof($date_to_check_array) != 3) {
    return false;
   }

   $size = sizeof($format_string_array);
   for ($i=0; $i<$size; $i++) {
    if ($format_string_array[$i] == 'mm' || $format_string_array[$i] == 'mmm') $month = $date_to_check_array[$i];
    if ($format_string_array[$i] == 'dd') $day = $date_to_check_array[$i];
    if ( ($format_string_array[$i] == 'yyyy') || ($format_string_array[$i] == 'aaaa') ) $year = $date_to_check_array[$i];
   }
  } else {
   if (strlen($format_string) == 8 || strlen($format_string) == 9) {
    $pos_month = strpos($format_string, 'mmm');
    if ($pos_month != false) {
     $month = substr( $date_to_check, $pos_month, 3 );
     $size = sizeof($month_abbr);
     for ($i=0; $i<$size; $i++) {
      if ($month == $month_abbr[$i]) {
       $month = $i;
       break;
      }
     }
    } else {
     $month = substr($date_to_check, strpos($format_string, 'mm'), 2);
    }
   } else {
    return false;
   }

   $day = substr($date_to_check, strpos($format_string, 'dd'), 2);
   $year = substr($date_to_check, strpos($format_string, 'yyyy'), 4);
  }

  if (strlen($year) != 4) {
   return false;
  }

  if (!settype($year, 'integer') || !settype($month, 'integer') || !settype($day, 'integer')) {
   return false;
  }

  if ($month > 12 || $month < 1) {
   return false;
  }

  if ($day < 1) {
   return false;
  }

with

function zen_checkdate($date, &$date_string) {
$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 (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;};
$date = $regs[$y] . $regs[$m] . $regs[$d];
if ($date < date(Ymd)+2) {$date_string = $date; return true;} else {return false;};
}

and voila, you should be able to enter dates like "27 february 82" etc. into the advanced search form.

PS. If you didn't already add the line ```
define('MONTH_ABBR', 'jan feb mar apr may jun jul aug sep oct nov dec');

29 Jan 2005, 12:25 AM
#13
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

..and while I'm raving about functions noone has ever heard of, isn't the following function completely useless? (also in functions_general.php, right after zen_checkdate)

 function zen_is_leap_year($year) {
  if ($year % 100 == 0) {
   if ($year % 400 == 0) return true;
  } else {
   if (($year % 4) == 0) return true;
  }

  return false;
 }

It would seem to me like simply ```
if (($year % 4) == 0) return true;


I'll shut up about functions for a while now ;)
31 Jan 2005, 2:17 PM
#14
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Remember that if you don't edit
admin-> configuration-> minimum values-> date of birth,
you will still need to enter dates with slashes, because
minimum value for date of birth is set to 10 at default, but that includes 2 slashes as well as 8 digits.

31 Jan 2005, 7:29 PM
#15
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Every time I think that something cannot be further improved, I end up proving myself wrong.

Today I learned of a better way of finding month names if the server supports the locale. The following code will let people enter month names even if the constant MONTH_ABBR is not defined (like already mentioned, the server must support the proper locale settings).

In both my function zen_date_raw and function zen_checkdate, replace the following lines

$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);};

with

if ((defined('MONTH_ABBR'))AND(constant('MONTH_ABBR'))) {$month_abbr = explode(" ", " " . strtolower(MONTH_ABBR));}
else {for ($i=1; $i < 13; $i++) {$month_abbr[$i] = strtolower(strftime("%b",strtotime("$i/1/2004")));}}

for ($i=1; $i < 13; $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;};};

This will see if the constant MONTH_ABBR is defined, if it's not then it will find out the month names for the language that is defined by setlocale(). This means that the constant MONTH_ABBR would only be needed for servers that don't support your locale.
setlocale() is set in includes/languages/<language>.php, around line 39, and it looks somewhat like this ```
@setlocale(LC_TIME, 'en_US');


To find out what locales your *nix server supports, you may try to add the following code in a test file:
<?php echo "<br />Locales availible on this server are:<br />"; system('locale -a'); // will list all locales availible on your server echo "<hr><br />English locales availible on this server are:<br />"; system('locale -a | grep en'); // will list all locales that contains "en" availible on your server ?>

For windows, [read this](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_country_strings.asp).

For more about setlocale, [read the manual](http://php.net/manual/en/function.setlocale.php)
1 Feb 2005, 4:19 PM
#16
theoracle avatar

theoracle

Suspended

Join Date:
Aug 2004
Posts:
3,180
Plugin Contributions:
1

Re: Function Zen_date_raw()

Perhaps it would be better to post one whole official code into another topic to avoid confusions. ;)

2 Feb 2005, 2:45 AM
#17
dwno avatar

dwno

Totally Zenned

Join Date:
Jan 2005
Posts:
1,285
Plugin Contributions:
0

Re: Function Zen_date_raw()

Done. :)
This thread has been moved here.

May I also add that the above post that mentions zen_checkdate is not entirely correct, see the other thread for the entire code that should be replaced.