• I want g:i a for events that start not-on-the-hour (for example 7:30 pm), but g a for events that start on the hour (like 7 pm).

    In computer-friendly grammar:

    If (event start) minutes = zero, <g a>, otherwise <g:i a>.

    If there’s no way to directly query if minutes=zero, is there a way to test something like:

    if (is whole number (datetime/24))?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Jay

    (@jaygumanid)

    To check if the minutes of an event start time are equal to zero, you can use the modulo operator % in combination with the date() function. The modulo operator returns the remainder of a division operation, so if the remainder of dividing the minutes by 60 is zero, it means the minutes are a multiple of 60, or in other words, equal to zero.

    Here’s an example of how you could use this to check if the minutes of an event start time are zero:

    if date('i', $event_start_timestamp) % 60 == 0:
      // event start time minutes are zero
    else:
      // event start time minutes are not zero

    To format the event start time in the desired format, you can use the date() function and specify the format string 'g:i a' for event start times with non-zero minutes and 'g a' for event start times with zero minutes.

    Here’s an example of how you could use this to format the event start time in the desired way:

    if date('i', $event_start_timestamp) % 60 == 0:
      $formatted_start_time = date('g a', $event_start_timestamp);
    else:
      $formatted_start_time = date('g:i a', $event_start_timestamp);

    This should give you the desired result of formatting the event start time as g:i a for events that start not-on-the-hour (e.g. 7:30 pm) and g a for events that start on the hour (e.g. 7 pm).

    Thread Starter Jonathon N

    (@imagiscapeca)

    Thank you! Code is Poetry! (Your answer is beautiful.)

    My next challenge is inserting that into 2 plugins I’m using – an event plugin and a datatables plugin. It seems I’ll need to get support from them for that.

    WordPress’s Settings > General > Time Format > Custom doesn’t accept a conditional statement. (The preview result was garbage.)

    I ran grep -r "g:i a" . (including the period) on the plugin folder and found some possible parts to replace, while keeping the ability to undo.

    $time_format = get_option( ‘time_format’, ‘g:i a’ );

    $template_vars[‘time_format’] = get_option( ‘time_format’, ‘g:i a’ );

    etc

    Must pause this project for a while. I’ll update later.

    Thread Starter Jonathon N

    (@imagiscapeca)

    P.S. I know we can override general settings formatting
    with code like
    Posted on <?php the_time( 'l, F jS, Y' ); ?>.
    (source:https://wordpress.org/support/article/formatting-date-and-time/#overriding-general-settings-formatting)

    I have not yet tested if the conditional code would work in that scenario.

    Moderator bcworkz

    (@bcworkz)

    You cannot place conditional code within a settings value. However you can likely conditionally filter the output. For example, there is “the_time” filter for use with calls to the_time(). Unfortunately, you cannot filter the $format string, but you can filter the actual time string that is to be output. Your filter callback could simply remove any “:00” it finds in time strings, assuming there will be no times such as “7:00:30 pm” 😉

    Of course “the_time” is only valid for calls using the_time(), but there are likely other filters for similar time string output functions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Conditional time formatting: want 7 pm not 7:00 pm if event starts on the hour.’ is closed to new replies.