Changing time format beyond normal customization

时间:2015-07-23 04:15:25

标签: php function date

We all know we can customize the time format for Wordpress using basic customizations as outlined here: https://codex.wordpress.org/Formatting_Date_and_Time

But, in order to match the styleguide a client uses for their printed materials, I've been asked for a different format than what can be achieved using the basic customizations.

Their styleguide states:

Times

  • Correct: 6p, 7 - 830a, 8a - 5p.
  • Never include the colon. (Do: 830 - 945p.)
  • If the time is at the top of the hour, drop the 00 at the end of the time. (Do: 8 - 9p.)
  • Use the lowercase letter “a” to indicate a.m. and the lowercase letter “p” to indicate p.m. with no spaces between the numbers and letters. (Do: 9 - 1030p.)
  • For spans of time, use a hyphen (-) with a space on each side of it. (Do: 8 - 930p.)
  • Never use noon or midnight. (Do: 12a or 12p.)
  • Avoid redundancies like morning or evening. (Don’t: 8a in the morning or 7p Tuesday evening.)

I am using using the Event Calendar Pro plugin as the calendar on the site and by using the custom time format of "gia" I can get the times to display as "July 25 @ 800am - 300pm" but I need to be able to drop the letter "m' from the "am" and "pm" and the "00" from times at the top of hours.

I'm assuming this can all be achieved with a little code in my functions.php file, but I'm not sure where to start, everything I've found in searching the forms regarding time formats just points me to the basic php time customizations.

Any help would be appreciated (besides telling me that the client's time formats are messed up and confusing to site users.)

1 个答案:

答案 0 :(得分:0)

You should write a function that displays your dates in the desired way.

function wpse195313_custom_date() {
    global $post;
    $theDate = $post->post_date;
    // check whether minutes are 00
    // use mysql2date function https://codex.wordpress.org/Function_Reference/mysql2date
    if ( mysql2date('i', $theDate, false) == 00 ) : 
        $outputDate = mysql2date('ga', $theDate, false);
    else 
        $outputDate = mysql2date('gia', $theDate, false);
    endif;
    echo $outputDate;
}

This should get you started, you'll need a bunch more conditionals to achieve what you are going for.