Zen Cart Logo
Forums / Zen Cart Code Suggestions / International Date Formats

International Date Formats

Locked

Views: 11,827

Results 41 to 60 of 71
This thread is locked. New replies are disabled.
10 Aug 2005, 12:59
#41
dwno avatar

dwno

Totally Zenned

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

International Date Formats

EssentialParadox:

So I should have what you have? Or do I have a choice of other things I should put?
You have a choice. As long as the letters d, m and y are present in one order or another it's ok. 'm/d/Y', 'd.m.y', 'd.m-Y' are some examples.

10 Aug 2005, 13:29
#42
essentialparadox avatar

essentialparadox

Zen Follower

Join Date:
May 2005
Posts:
89
Plugin Contributions:
0

Re: International Date Formats

dwno:

You have a choice. As long as the letters d, m and y are present in one order or another it's ok. 'm/d/Y', 'd.m.y', 'd.m-Y' are some examples.
Doesn't change anything :(

But.. I am having a problem with another date module I added - the drop down date selector.

Considering both these modules aren't working, I feel It's because they clash.

If you're quite familiar with the international date format code, then here's the drop down menu code changes. If you could take a quick look to see if it clashes, that would be really appreciated:

**For Versions v1.2.3 and above:

Dropdown menu for dob when create an account. short and easy.

Only five files are changed:

/includes/templates/template_default/templates/tpl_login_default.php
/includes/templates/template_default/templates/tpl_create_account_default.php
/includes/modules/pages/create_account/header_php.php
/includes/modules/pages/account_edit/header_php.php
/includes/templates/template_default/templates/tpl_account_edit_default.php

/includes/modules/pages/create_account/header_php.php (around line 34)
/includes/modules/pages/account_edit/header_php.php (around line 32)

=======

FIND THIS CODE:

if (ACCOUNT_DOB == 'true') $dob = zen_db_prepare_input($_POST['dob']);

AND REPLACE WITH THIS CODE:

//MOD Date of Birth Dropdowns v1.0d
if (ACCOUNT_DOB == 'true') $dob = zen_db_prepare_input($_POST[dob_month] . '/' . $_POST[dob_day] . '/' . $_POST[dob_year]);
//MOD_eof Date of Birth Dropdowns v1.0d

======================================================================

/includes/templates/template_default/templates/tpl_create_account_default.php (around line 84)
/includes/templates/template_default/templates/tpl_login_default.php (around line 141)

======================================================================

FIND THIS CODE:

    <td class="main"><?php echo zen_draw_input_field('dob') . '' . (zen_not_null(ENTRY_DATE_OF_BIRTH_TEXT) ? '<span class="inputRequirement">' . ENTRY_DATE_OF_BIRTH_TEXT . '</span>': ''); ?></td>

AND REPLACE WITH THIS CODE:

<?php //MOD Date of Birth Dropdowns v1.0d ?>
   <td class="main">
    <?php
    
for ($i=1; $i<32; $i++) {
$dob_day_array[] = array('id' => sprintf('%02d', $i), 'text' => sprintf('%02d', $i));

}

for ($i=1; $i<13; $i++) {
$dob_month_array[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
}

$today = getdate();
$first_year = $today['year'] - 100;
$last_year = $today['year'] - 15;
for ($i=$first_year; $i < $last_year; $i++) {
$dob_year_array[] = array('id' => sprintf('%02d', $i), 'text' => sprintf('%02d', $i));
}

echo zen_draw_pull_down_menu('dob_month', $dob_month_array);
echo zen_draw_pull_down_menu('dob_day', $dob_day_array);
echo zen_draw_pull_down_menu('dob_year', $dob_year_array);
echo '' . (zen_not_null(ENTRY_DATE_OF_BIRTH_TEXT) ? '<span class="inputRequirement">' . ENTRY_DATE_OF_BIRTH_TEXT . '</span>': '');
?>
</td>

<?php //MOD_eof Date of Birth Dropdowns v1.0d ?>

======================================================================

/includes/templates/template_default/templates/tpl_account_edit_default.php (around line 65)

======================================================================

FIND THIS CODE:

    <td class="main"><?php echo zen_draw_input_field('dob') . '' . (zen_not_null(ENTRY_DATE_OF_BIRTH_TEXT) ? '<span class="inputRequirement">' . ENTRY_DATE_OF_BIRTH_TEXT . '</span>': ''); ?></td>

AND REPLACE WITH THIS CODE:

<?php //MOD Date of Birth Dropdowns v1.0d ?> <td class="main"> <?php for ($i=1; $i<32; $i++) { $dob_day_array[] = array('id' => sprintf('%02d', $i), 'text' => sprintf('%02d', $i)); }

for ($i=1; $i<13; $i++) {
$dob_month_array[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
}

$today = getdate();
$first_year = $today['year'] - 100;
$last_year = $today['year'] - 15;

for ($i=$first_year; $i < $last_year; $i++) {
$dob_year_array[] = array('id' => sprintf('%02d', $i), 'text' => sprintf('%02d', $i));
}

$dob_raw = $account->fields['customers_dob'];
$dob_year_val = substr($dob_raw, 0, 4);
$dob_month_val = (int)substr($dob_raw, 5, 2);
$dob_day_val = (int)substr($dob_raw, 8, 2);

echo zen_draw_pull_down_menu('dob_month', $dob_month_array, $dob_month_val);
echo zen_draw_pull_down_menu('dob_day', $dob_day_array, $dob_day_val);
echo zen_draw_pull_down_menu('dob_year', $dob_year_array, $dob_year_val);
echo '' . (zen_not_null(ENTRY_DATE_OF_BIRTH_TEXT) ? '<span class="inputRequirement">' . ENTRY_DATE_OF_BIRTH_TEXT . '</span>': '');
?>

</td> <?php //MOD_eof Date of Birth Dropdowns v1.0d ?>**
10 Aug 2005, 15:23
#43
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

After a brief look at that I can't say why they should clash.

I have a feeling you're just missing something, so try it again from the beginning and follow the instructions carefully. Then test it and make sure it works before adding another contribution, just to make sure there's no clash.

It should just work, as far as I can see.

10 Aug 2005, 15:24
#44
essentialparadox avatar

essentialparadox

Zen Follower

Join Date:
May 2005
Posts:
89
Plugin Contributions:
0

Re: International Date Formats

dwno:

After a brief look at that I can't say why they should clash.

I have a feeling you're just missing something, so try it again from the beginning and follow the instructions carefully. Then test it and make sure it works before adding another contribution, just to make sure there's no clash.

It should just work, as far as I can see.
OK, I'll start again from scratch. Thanks.

13 Aug 2005, 00:37
#45
essentialparadox avatar

essentialparadox

Zen Follower

Join Date:
May 2005
Posts:
89
Plugin Contributions:
0

Re: International Date Formats

Working great now! It's the additional modifications to change the advanced search where I had a problem. I'd tried using an override folder for the file changes, but I guess that doesn't work everywhere in the file structure. :)

Thanks for the great addition

now... how do I change the default text in the advanced search date boxes? :)

13 Aug 2005, 12:20
#46
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

EssentialParadox:

now... how do I change the default text in the advanced search date boxes? :)
includes/languages/<your language>.php

Search for: DOB_FORMAT_STRING

Glad you finally got it to work :)

06 Oct 2005, 12:11
#47
suehutton avatar

suehutton

New Zenner

Join Date:
Mar 2005
Location:
UK
Posts:
37
Plugin Contributions:
0

Re: International Date Formats

Hello

I've been trying to follow this discussion. All I want to do is to change the date format to dd/mm/yyyy to suit UK usage.

This is a clip from my includes/languages/english.php file

**
// look in your $PATH_LOCALE/locale directory for available locales..
// on RedHat try 'en_US'
// on FreeBSD try 'en_US.ISO_8859-1'
// on Windows try 'en', or 'English'
@setlocale(LC_TIME, 'en_GB.ISO_8859-1');
define('DATE_FORMAT_SHORT', '%d/%m/%Y'); // this is used for strftime()
define('DATE_FORMAT_LONG', '%A %d %B, %Y'); // this is used for strftime()
define('DATE_FORMAT', 'd/m/Y'); // this is used for date()
define('DATE_TIME_FORMAT', DATE_FORMAT_SHORT . ' %H:%M:%S');

////
// Return date in raw format
// $date should be in format mm/dd/yyyy
// raw date is in format YYYYMMDD, or DDMMYYYY
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);
}
}
**

Do I have to change the zen_date_raw function somehow, to get the result that I want? I don't want all the code that enables people to put in full month names etc.

Or have I got the locale wrong? I don't know how I would find the $PATH_LOCALE/locale directory on my hosting server. I tried phpinfo, but it doesn't appear to be there.

Could you please advise? I'd be very grateful. Thank you for your enthusiasm and contributions.

06 Oct 2005, 12:28
#48
essentialparadox avatar

essentialparadox

Zen Follower

Join Date:
May 2005
Posts:
89
Plugin Contributions:
0

Re: International Date Formats

What's the harm in putting in the extra code for full month names, etc ? You could just put it in and not use it.

Do you have a US host? If you do, then it's unlikely they support a UK locale. I would put it as a US locale, or talk to your host.

06 Oct 2005, 12:42
#49
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

If you just want it simple, try this:

// raw date is in format YYYYMMDD, or DDMMYYYY
function zen_date_raw($date, $reverse = false) {
 if ($reverse) {
  return substr($date, 0, 2) . substr($date, 3, 2) . substr($date, 6, 4);
 } else {
  return substr($date, 6, 4) . substr($date, 3, 2) . substr($date, 0, 2);
 }
}

Then you'll still have to insert slashes btw.

To find out the correct locale, you could try asking your host if you can't figure it out yourself ;)

11 Oct 2005, 01:11
#50
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

.. and for anyone wanting simple yyyy/mm/dd format, try this:

// raw date is in format YYYYMMDD, or DDMMYYYY
function zen_date_raw($date, $reverse = false) {
 if ($reverse) {
  return substr($date, 8, 2) . substr($date, 5, 2) . substr($date, 0, 4);
 } else {
  return substr($date, 0, 4) . substr($date, 5, 2) . substr($date, 8, 2);
 }
}
11 Oct 2005, 08:06
#51
suehutton avatar

suehutton

New Zenner

Join Date:
Mar 2005
Location:
UK
Posts:
37
Plugin Contributions:
0

Re: International Date Formats

Thanks. I can see how it works now.

13 Oct 2005, 21:33
#52
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

What do coder-geeks do when they have some spare time?
-We code :lol:

Been a while since I wrote the more advanced date function in this thread, and looking back at it, it doesn't look good enough to me. B)
I was considering using it outside ZC, so I decided to clean it up and make it better while at it.

Just for fun, I wrote something that will automatically find the correct DATE_FORMAT too, if it's not already set, based on the set locale.

That could probably be used by some other modules too btw..

Added more letters to be used with DATE_FORMAT too, as d, m and y may not be exactly what everyone want to use.

Now handles: Y, y, M, m, n, d, j - Explanations

function zen_date_raw($date) {
  $date = strtolower($date);
  
  // get a list of abbreviated month names
  for ($i=1; $i < 13; $i++) {
    $month_abbr[$i] = strtolower(strftime('%b',strtotime("2000-$i-1")));
  }
  
  // if DATE_FORMAT has not been defined, find it automatically by checking against the localised format
  if (!defined('DATE_FORMAT')) {
    $local_format = strftime('%x', strtotime('2000-1-3'));
    
    $local_format = str_replace(array('2000', '00', '01', '1', '03', '3'), array('Y', 'y', 'm', 'n', 'd', 'j'), $local_format);
    $local_format = str_replace($month_abbr, 'M', $local_format);
    define('DATE_FORMAT', $local_format);
  }
  
  // find the first occurance of a month name
  // this is necessary since we might be using abbreviations, and there's a chance one abbreviation is part of another month name. thus, if the customer is entering the full month name, he would not get the correct month if we didn't do this
  for ($i=1; $i < 13; $i++) {
    $month_strpos[$i] = strpos($date, $month_abbr[$i]);
    if ($month_strpos[$i] !== false && (!isset($first_case) || $month_strpos[$i] < $first_case)) {
      $first_case = $month_strpos[$i];
      $m = $i;
    }
  }
  if (isset($m) && isset($first_case)) {
    // insert month number in front of the string where month name was found
    $date = substr_replace($date, $m, $first_case, 0);
  }
  
  // remove all non-numeric characters
  $date = preg_replace('/[^0-9]/','', $date);
  
  // remove all characters except those allowed from the constant DATE_FORMAT
  $dformat = preg_replace('/[^YyMmndj]/','', 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 (preg_match('/(d|j)(M|m|n)(y|Y)/', $dformat)) {
    $regexp = $dd . $mm . $yyyy;
    $d = '1';
    $m = '2';
    $y = '3';
  }
  elseif (preg_match('/(M|m|n)(d|j)(y|Y)/', $dformat)) {
    $regexp = $mm . $dd . $yyyy;
    $d = '2';
    $m = '1';
    $y = '3';
  }
  elseif (preg_match('/(d|j)(y|Y)(M|m|n)/', $dformat)) {
    $regexp = $dd . $yyyy . $mm;
    $d = '1';
    $m = '4';
    $y = '2';
  }
  elseif (preg_match('/(M|m|n)(y|Y)(d|j)/', $dformat)) {
    $regexp = $mm . $yyyy . $dd;
    $d = '4';
    $m = '1';
    $y = '2';
  }
  elseif (preg_match('/(y|Y)(d|j)(M|m|n)/', $dformat)) {
    $regexp = $yyyy . $dd . $mm;
    $d = '3';
    $m = '4';
    $y = '1';
  }
  else {
    $regexp = $yyyy . $mm . $dd;
    $d = '4';
    $m = '3';
    $y = '1';
  }
  
  // if this doesn't look like a valid date, return false
  if (!preg_match('/^' . $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];
    }
  }
  
  // validate the date
  if (!checkdate($regs[$m], $regs[$d], $regs[$y])) {
    return false;
  }
  
  // return YYYYMMDD
  return $regs[$y] . $regs[$m] . $regs[$d];
}

This may look like much more than the previous function I wrote, but this one is formatted with space and line breaks for easier reading, while the old one was compressed and difficult to read.

18 Oct 2005, 02:05
#53
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

Someone asked me if I could upload the code as a file instead, so I did just that.

You can get it here

It's distributed as GPL, and with another name than zen_date_raw, to illustrate it could be used in other projects too.

Just rename the function in the file to zen_date_raw for use with Zen Cart, like it states in the file.

27 Nov 2005, 09:29
#54
stenrique avatar

stenrique

Zen Follower

Join Date:
Apr 2005
Posts:
355
Plugin Contributions:
0

Re: International Date Formats

Hi, I've made all the amendments mentioned in the postings thus far, I've also did the follows: > **PS. For those that are interested; I know there's at least one place in the admin that depends on date() to display the date, but that's not good for internationalisation, because unlike strftime(), date() does not rely on locale. This means that date() will only show english month and day names etc. while strftime() will show localised day and month names. The case I'm aware of at the time, is in admin/includes/header.php on line 153. You may want to replace

CODE

<?php echo date("r", time()) . 'GMT' . '[' . $_SERVER['REMOTE_ADDR'] . ' ]'; ?>

with something like
CODE

<?php echo strftime("%c") . '[' . $_SERVER['REMOTE_ADDR'] . ' ]'; ?>**

However, I am not getting the dates in admin to display in dd/mm/yy format (not just in the header but also in the "New orders" and "New Customers" fields.).

I'm currently running my development in windows Localhost. Does this has any bearing on the date display format? Great contrib. dwno. What I've been looking for. Thanks.

27 Nov 2005, 14:24
#55
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

Open admin/includes/languages/<current language>.php
You need to replace the following

define('DATE_FORMAT_SHORT', '%m/%d/%Y'); // this is used for strftime()
define('DATE_FORMAT_LONG', '%A %d %B, %Y'); // this is used for strftime()
define('DATE_FORMAT', 'm/d/Y'); // this is used for date()
define('PHP_DATE_TIME_FORMAT', 'm/d/Y H:i:s'); // this is used for date()
define('DATE_TIME_FORMAT', DATE_FORMAT_SHORT . ' %H:%M:%S');
define('DATE_FORMAT_SPIFFYCAL', 'MM/dd/yyyy'); //Use only 'dd', 'MM' and 'yyyy' here in any order

with something like

define('DATE_FORMAT_SHORT', '%d/%m/%Y'); // this is used for strftime()
define('DATE_FORMAT_LONG', '%A %d %B, %Y'); // this is used for strftime()
define('DATE_FORMAT', 'd/m/Y'); // this is used for date()
define('PHP_DATE_TIME_FORMAT', 'd/m/Y H:i:s'); // this is used for date()
define('DATE_TIME_FORMAT', DATE_FORMAT_SHORT . ' %H:%M:%S');
define('DATE_FORMAT_SPIFFYCAL', 'dd/MM/yyyy'); //Use only 'dd', 'MM' and 'yyyy' here in any order

That goes for includes/languages/<current language>.php too if you haven't done so already.

27 Nov 2005, 17:26
#56
stenrique avatar

stenrique

Zen Follower

Join Date:
Apr 2005
Posts:
355
Plugin Contributions:
0

Re: International Date Formats

You are absolutely right dwno, I've missed out the modification for the english.php in admin. Incorporate as suggested and voila, the dates are displayed in the set format.

However, I've replaced the coding as suggested in your 2nd posting under PS. but the date still shows as mm/dd/yy. Did I miss out something again?

27 Nov 2005, 17:57
#57
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

You also need to set setlocale() correctly for that to work. :)
setlocale() is also set in includes/languages/<current language>.php

I've already written the following directions somewhere else, but I'll copy and paste them here too.

setlocale() can be tricky to set correctly, because it depends on the servers operating system.
If you're running on a windows platform, see Language Strings or Country/Region Strings from the MSDN site.
If you're running on a *nix system, see the directory /usr/share/locale/ and try one of the directory names that match your language for setlocale(). If you can't access this directory, ask your host about what locales they have installed for your language.

Or you could just try something like

<?php echo strftime(DATE_FORMAT_LONG) . '[' . $_SERVER['REMOTE_ADDR'] . ' ]'; ?>

instead of the suggested

<?php echo strftime("%c") . '[' . $_SERVER['REMOTE_ADDR'] . ' ]'; ?>

in that post.

If you don't set setlocale() correctly, you won't have localised settings such as month names, but the default US english instead.

28 Nov 2005, 02:45
#58
stenrique avatar

stenrique

Zen Follower

Join Date:
Apr 2005
Posts:
355
Plugin Contributions:
0

Re: International Date Formats

Thanks dwno, I've tried w/o success to change the setlocale. Hence I opt to replaced the code to > <?php echo strftime(DATE_FORMAT_LONG) . '[' . $_SERVER['REMOTE_ADDR'] . ' ]'; ?>
The date display looks so much better now :D

28 Nov 2005, 02:55
#59
dwno avatar

dwno

Totally Zenned

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

Re: International Date Formats

Just a note. setlocale also needs to be set in both includes/languages/<current language>.php and
admin/includes/languages/<current language>.php

Or rather in the override directories:
includes/languages/<current template>/<current language>.php and
admin/includes/languages/<current template>/<current language>.php

..just thought I'd mention it in case that's why you didn't manage to set it. :)

28 Nov 2005, 18:41
#60
stenrique avatar

stenrique

Zen Follower

Join Date:
Apr 2005
Posts:
355
Plugin Contributions:
0

Re: International Date Formats

Oh my, why didn't it occur to me ::frust . Changed the english.php in admin and done :)

dwno, you deserve an award. ::tup