Page 1 of 1
International days of week, non-specific date
Posted: Thu Mar 27, 2014 12:59 pm
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.
Re: International days of week, non-specific date
Posted: Thu Mar 27, 2014 4:15 pm
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.
Re: International days of week, non-specific date
Posted: Thu Mar 27, 2014 4:35 pm
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.
Re: International days of week, non-specific date
Posted: Thu Mar 27, 2014 4:48 pm
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.
Re: International days of week, non-specific date
Posted: Thu Mar 27, 2014 5:07 pm
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.
Re: International days of week, non-specific date
Posted: Thu Mar 27, 2014 10:01 pm
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.
Re: International days of week, non-specific date
Posted: Fri Mar 28, 2014 5:39 pm
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.