International days of week, non-specific date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

International days of week, non-specific date

Post by rhecker »

I am using the IntlDateFormatter class with the PHP DateTime class to format dates in different languages. It's great for specific dates. For example, the following returns the Vietnamese for the day of the week:

Code: Select all

$date = new DateTime();
$formatter = new IntlDateFormatter('vi', IntlDateFormatter::LONG, NULL, 'America/Los_Angeles', NULL, "EEEE");
$dayofweek = $formatter->format($date);
But what if I want to return the names of days-of-the-week and months without referring to a specific date? For instance, "Monday, Wednesday, Friday?"

I can think of an inelegant solution to this. December, 2013 started on a Sunday, so I can use it as my "proxy" date, like so: new DateTime(2013-12-$dow), where $dow is the day of the week, from 1 to 7. If the value of $dow is 3, Wednesday would be returned, but if there is a way for the IntlDateFormatter (or DateTime) to work with non-specific dates, it would be a better solution.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: International days of week, non-specific date

Post by requinix »

It doesn't even matter what date you start with so long as you do 7 of them and associate them with the right day. But to the question yeah, that's basically what I do too.

Or you can take the simple way and just store them in an array - it's not like the day names are going to change, and if you wanted to switch language you'd have to alter the code anyways.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: International days of week, non-specific date

Post by rhecker »

. . .and if you wanted to switch language you'd have to alter the code anyways.
No, the whole idea is for my clients to be able to add as many languages to their sites as they need, without my involvement. This is the beauty of the IntDateFormatter class (and to a lesser degree, set_locale). It only requires the two character identification for the language: en, de, zh, vi, etc. So the solution must be maintenance free.

Other aspects of multi-language sites I already have under control. Dates are the last issue.
Last edited by rhecker on Thu Mar 27, 2014 5:04 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: International days of week, non-specific date

Post by Christopher »

Find the weekday of the 1st of the month in a language you know. Then get the names for the 1st through the 7th in the requested language. Fill an array with the names, putting them in the right indexes 1..7.

You could also cache the language in a file -- serialized or JSON. Write a class that loads the file and returns the days of the week for a given language. If the language is not found in the array then find the names of the week (as above), add them to the array, and write the whole array back to the file.
(#10850)
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: International days of week, non-specific date

Post by rhecker »

Christopher, that's the solution I already described. (your paragraph 1). Your suggestion in paragraph 2 wouldn't work because it requires that I create the array for each language.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: International days of week, non-specific date

Post by requinix »

rhecker wrote:Your suggestion in paragraph 2 wouldn't work because it requires that I create the array for each language.
He's talking about caching the results. You'd still have to generate the day names like before but rather than do that every single time you want the days, you do it once, cache it somewhere, and read that instead. It's the benefits of being able to handle every language as well as not losing out on performance checking something that never changes.

It's as easy as, like,

Code: Select all

<?php

// $days is an array of day names in some $language
file_put_contents("days.{$language}.php", "<?php return " . var_export($days, true) . ";");
If the "days.*.php" file exists you can simply $days = include(...) it.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: International days of week, non-specific date

Post by rhecker »

OK, but that wasn't the question. I wasn't asking to solve a performance issue.

It's OK if no one has a better solution than what I came up with myself. I just wondered if there was a better solution out there than the one I had already thought of.
Post Reply