date_time_selector, need 12 hour format

date_time_selector, need 12 hour format

by Charles McDonald -
Number of replies: 6

So I'm in the FormAPI, using the date_time_selector. (I'm using Moodle 3.1.2)

      And I'm stuck - can anyone help?


A project I'm working on requires 12-hour formatted time.

      The date_time_selector does 24-hour time great - and would be my preferred way to do this.

      But I need 12-hour time. Is there a way to get 12-hour format in Moodle?


If not, can anyone recommend a good PHP date_time_selector like snippet that can to 12-hour format?


Thank you so much for considering!

    -- Chuck

  

Average of ratings: -
In reply to Charles McDonald

Re: date_time_selector, need 12 hour format

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Are you looking for 12 hours, with AM/PM?

You may well be able to take a copy of the selector in lib/form/datetimeselector.php, put it somewhere within your plugin, rename it to something sensible and change it (slightly) to display 12 hours instead of 24 hours.

You need to add a 'registerElementType' call somewhere (take a look at the bottom of lib/formslib.php) - I suggest you add this to the bottom of the file where you define the new form element (or you could find another sensible location to put it).

Once you've done that, you should be able to add it to your page, like any other element.

In reply to Davo Smith

Re: date_time_selector, need 12 hour format

by Charles McDonald -

I like the API date_time_selector so would like to keep that format.

    And yes, 12-Hour format would necessitate an A/P drop-down selector.

In reply to Charles McDonald

Re: date_time_selector, need 12 hour format

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I can imagine someone thinking they need 12-hour format, but I am finding it harder to thinke why it would really be essential. Is the example you have interesting, and can you share it?

Does the date-selector respspect the date-time display settings in langconfig.php?

In reply to Tim Hunt

Re: date_time_selector, need 12 hour format

by Charles McDonald -

I work at a private, New England boarding school and we have a number of dorm-related features build into Moodle as Plugins.

     Perhaps this goes beyond Moodle's original intent but it works well for our students and the integration with their courses is awesome. So specifically, we have a signout form for the holidays in which students share their travel plans including when they leave and when they return. For most students, 24-hour time is no big deal but until you can choose 12 or 24 hour format as a profile option, we need to be able to offer it for those few students who are unfamiliar with 24 hour time.

     Now some of you may want to reply that this is a user issue not a moodle issue and I am inclined to agree with you there but that's not how good customer service works so we need to meet our users where they are and not where we'd like them to be.

In reply to Charles McDonald

Re: date_time_selector, need 12 hour format

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Thanks for explaining

Probably the simplest fix would be to change the values in the Hours dropdown, so instead of 0 - 23, you have

12 am, 1am, 2am, ... 11 pm, 12 pm, 1pm, ... 11 pm

I know that is not where you would normally put the am/pm if you were writing the date out, but it is easy to do. You just need to modify https://github.com/moodle/moodle/blob/master/lib/form/datetimeselector.php#L131.

Yes, changing core code is dangerous, but in this case, it as going to be by far the easiest way to achieve this features.


In reply to Tim Hunt

Re: date_time_selector, need 12 hour format

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

I would think its more a matter of preference. While it is common to use 24 hour format in Europe, its is very uncommon in North America. North Americans still use AM/PM instead.