How to get "first day of week" in php to match jquery datepicker's locale.firstDay?

In jquery you can get a user's locale (depending on language set by the user or site) with

var locale = $.datepicker.regional[elgg.get_language()];

and then initialize the datepicker accordingly. Depending on the language the calendar will be displayed with the weeks starting either on Sunday or Monday.

The problem I now have is that I need to know also in php (even before initialization of the datepicker) if the weeks start on Sunday or Monday (as displayed for the users in their language).

While I can find out about the user/site language also in php using elgg_get_language() of the Elgg API I have no idea of how to make use of the language set. With date("w") I would be able to get the first day of a week if this day is to be a Sunday and with date("N") I would be able to do the same if the day is to be a Monday. But which arguement to use if you only know the language (and not even knowing which day of the week is traditionally the first day of week in the corresponding countries)?

To know about the first day of a week would be necessary to calculate a date range (the week) for a given date. I would need that in Event Calendar for the listing of events for a given week and then also to define the days to be highlighted in the calendar (and here we are again at the datepicker which requires to be provided with the days/dates to be highlighted from php).

  • I say, don't bother. Use the default setting for your country, and allows users to change it if they want.
    It's still on my to do list to write a plugin that would allow users to configure their timezone, preferred date and time formats, etc. For it to work properly, core would need to stop assuming any specific date formats though.
    In any case, you can pass the start of the week setting to datepicker view with datepicker_opts (I think that's what we named it)

  • I was afraid to get this answer... :-)

    User setting (or even just a plugin setting) would be one possibility. Though it might result in different calendar configs being used by different plugins (and Elgg core). Though I might have had a wrong memory about datepicker usage in Elgg core as it seems the firstDay parameter isn't set at all to be language depending.

    I guess one other possibility is to just extract the firstDay values for the different languages defined in jquery.ui i18n, put them in an array and check the site/user language shortcut against the array key/values. Maybe not very elegant but it should work.

  • Seriously, is it worth the effort? How many US users do you have and how many of them can't deal with weeks starting on Monday like in the rest of the civilized world? Don't waste your time until there is a financial benefit to do so, then hire me to do it for you ;)

  • Actually, most of my users are from the US. But it's rather the other way round: the current implementation in Event Calendar is with Sunday as first day of the week. So, I want to add the support for Monday as week starter (for the majority of other countries indeed).

    I've extracted the list of language codes with the firstDay values from datepicker i18n files now and added a function that returns the language-specific firstDay value to be used. In theory I would be done but as it happens I've found at least one (new) bug I've most likely added on a code cleanup/rewrite recently. Fix one, get one for free... I guess I know what to do on the weekend. :-(