Change all date formats to UK rather than USA

Open to all! Ask other shopowners for help.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Change all date formats to UK rather than USA

Post by 14Steve14 »

Is there a simple way to change things so that all dates in both the admin and store front show in a UK format. I have changed the includes/language/english files as per the user guide but everything still shows in a US format.

Here is what I currently have in the admin backend and the store front is very similar apart from the date picker code.

Code: Select all

setlocale(LC_ALL, ['en_GB.UTF-8', 'en_GB.UTF8', 'eng_gb']);
const DATE_FORMAT_SHORT = '%d/%m/%Y';  // this is used for strftime()
const DATE_FORMAT_LONG = '%A %d %B, %Y'; // this is used for strftime()
const DATE_FORMAT = 'd/m/Y'; // this is used for date()
const PHP_DATE_TIME_FORMAT = 'd/m/Y H:i:s'; // this is used for date()
const DATE_TIME_FORMAT = DATE_FORMAT_SHORT . ' %H:%M:%S';
const JQUERY_DATEPICKER_I18N_CODE = 'en-GB'; // leave empty for en_US; see http://jqueryui.com/demos/datepicker/#localization
const JQUERY_DATEPICKER_FORMAT = 'dd/mm/yy'; // see http://docs.jquery.com/UI/Datepicker/formatDate


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
burt
Core Team
Posts: 4561
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 422 times

Re: Change all date formats to UK rather than USA

Post by burt »

Note that, very soon, I'll be changing the Date Input on create_account.php to the HTML5 version which changes the date input based on the language of the users browser, eg;

Someone in the USA would see (and input);
mm/dd/yyyy

Whereas someone in the UK would see (and input);
dd/mm/yyyy

And so on for different languages. Correct error messages etc are formed by the browser like the rest of create_account.php

If you want to try it, see viewtopic.php?p=12993#p12993 (note that this does require a DB change from DATETIME to DATE for the customers [ ALTER TABLE customers CHANGE customers_dob customers_dob DATE NULL DEFAULT NULL; ] - use phpmyadmin). <<<<<------ this SQL change is important.

Test it on a TEST SHOP, also making sure to uninstall the header_tag module datepicker_jquery [or whatever it's called].
I am not here to build for you.
I am here to build with you. Let's help each other.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Change all date formats to UK rather than USA

Post by ecartz »

14Steve14 wrote: Sat Sep 16, 2023 2:39 pm everything still shows in a US format.
Can you send me a link to see a place where it is showing a US format on your store?
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Change all date formats to UK rather than USA

Post by 14Steve14 »

ecartz wrote: Sat Sep 16, 2023 8:35 pm
14Steve14 wrote: Sat Sep 16, 2023 2:39 pm everything still shows in a US format.
Can you send me a link to see a place where it is showing a US format on your store?
Thanks for that.

The database is a new one that was created when I started work on this test shop. I have not imported any customer or product data or orders. Its still as it was created when installed.

Its mainly the backend where there are differences in the way the dates are displayed. In the customers main page the date the account was created is showing correct as UK format.

On the orders page and on the orders status page the date is showing in US format. I have attached images.

The only thing I can see on the cart side is the DOB, but we don't use that so its no big thing.

I hope the images help.

Steve
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Change all date formats to UK rather than USA

Post by ecartz »

Note that only one of those dates is showing as US format: the 9/13/2023 (month/day/year). I think that Burt was suggesting a change to the date of birth input that might fix that. The rest are the international format (year-month-day). That's just blatting out what the database has. You could go through and wrap those in Date::expound if you wanted. E.g. https://github.com/CE-PhoenixCart/Phoen ... t.php#L180

Code: Select all

                  echo '<td>' . Date::expound($orders_history['date_added']) . '</td>';
Note that this isn't new. That goes back to version 1.0.6.0 when bootstrapping happened: https://github.com/CE-PhoenixCart/Phoen ... 1dc9eabe22
User avatar
burt
Core Team
Posts: 4561
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 422 times

Re: Change all date formats to UK rather than USA

Post by burt »

I do recall making a (kind of) arbitrary decision to just output (for dates in the admin) whatever was stored in the DB. As most dates (not all) are stored as so; yyyy-mm-dd hh:mm:ss so that is what is output.

The decision on that was made to try to make dates OK worldwide (ie if someone is in USA they see yyyy-mm-dd hh:mm:ss and if someone is in UK they also see yyyy-mm-dd hh:mm:ss). I thought in the admin area this wouldn't really matter?

For dates that customers can see, I *think* (though I am not sure as I can't recall for certain) that these can be localised, eg yyyy-mm-dd hh:mm:ss can be shown as 25/12/2022 for UK or 12/25/2022 for US. Eg shop side order_history, admin side invoice etc
I am not here to build for you.
I am here to build with you. Let's help each other.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Change all date formats to UK rather than USA

Post by 14Steve14 »

I think with the help from this post, and a bit of digging through other sections of code I have all the dates formatted how I want them all apart from one area. That is the section on the customers orders edit page at the bottom that shows the Payment method and payment type dates. The code I believe is in in admin/includes/actions/orders/edit.php

Code: Select all

<tr>
              <td><p><?= sprintf(TEXT_ORDER_PAYMENT, $order->info['payment_method'], $order->info['date_purchased']) ?></p></td>
              <td><p><?= sprintf(TEXT_ORDER_STATUS, $order->info['orders_status'], ($order->info['last_modified'] ?? $order->info['date_purchased'])) ?></p></td>
              <td><h1 class="display-4"><?= $order->info['total'] ?></h1></td>
            </tr>
Is there anyone that can help change that bit of code in two places to show UK date and time please.
User avatar
burt
Core Team
Posts: 4561
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 422 times

Re: Change all date formats to UK rather than USA

Post by burt »

If you used the Date::expound method mentioned by Matt, do the same in this file;

Date::expound($order->info['date_purchased'])
I am not here to build for you.
I am here to build with you. Let's help each other.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Change all date formats to UK rather than USA

Post by 14Steve14 »

burt wrote: Wed Sep 20, 2023 11:32 am If you used the Date::expound method mentioned by Matt, do the same in this file;

Date::expound($order->info['date_purchased'])
Sorry but I missed a bit out of my last post. The date needs to show the time also. I get the expound change, and I get that abridge gives a different date, but how do you show the date and time. I can see in other places that something like the below code is used, but I cannot make that work in the code that needs updating.

Code: Select all

(new Date($table_definition['info']->date_purchased))->format(DATE_TIME_FORMAT

Code: Select all

<td><p><?= sprintf(TEXT_ORDER_PAYMENT, $order->info['payment_method'], $order->info['date_purchased']) ?></p></td>
I hope that makes sense.
User avatar
burt
Core Team
Posts: 4561
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 422 times

Re: Change all date formats to UK rather than USA

Post by burt »

Code: Select all

<td><p><?= sprintf(TEXT_ORDER_PAYMENT, $order->info['payment_method'], $order->info['date_purchased']) ?></p></td>
Becomes

Code: Select all

<td><p><?= sprintf(TEXT_ORDER_PAYMENT, $order->info['payment_method'], (new Date($order->info['date_purchased']))->format(DATE_TIME_FORMAT)) ?></p></td>
Which changes the output from;

2023-09-20 11:05:40

to

09/20/2023 11:05:40

Now you need to format that date to swap around the month and day.
/admin/includes/languages/english.php

Search for DATE_TIME_FORMAT, you can see this is found early on in the file;

Code: Select all

const DATE_TIME_FORMAT = DATE_FORMAT_SHORT . ' %H:%M:%S';
Part of it is created by DATE_FORMAT_SHORT, so search for that. Even earlier in the file;

Code: Select all

const DATE_FORMAT_SHORT = '%m/%d/%Y';
Swap the d and the m.

That may (or may not) cause unintended side effects, but it should be OK to do.
I am not here to build for you.
I am here to build with you. Let's help each other.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply