Relative dates in wordpress



  • I'll use the wp relative date to remove the likes of today, yesterday. But there's a problem with dates later, there's brackets.

    11.12.2015 () 22:10

    code

       $output = $before.$the_date.' ('.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).')'.$after;
                }
    

    I didn't try to clean it up, but it didn't work. Just like to make sure that when this date was the day before yesterday 13.12.2015 doesn't show time, it's time to use the simple...time.

    <div class="meta">
                <?php relative_post_the_date(); ?> <?php the_time( $d ); ?> 
            </div>
    

    The function itself looks like that.

    ### Alternative To WordPress the_date().
    function relative_post_the_date($d = '', $before = '', $after = '', $display_ago_only = false, $display = true) {
        global $post;
        if (empty($d)) {
            $the_date = mysql2date(get_option('date_format'), $post->post_date);
        } else {
            $the_date = mysql2date($d, $post->post_date);
        }
        if(gmdate('Y', current_time('timestamp')) != mysql2date('Y', $post->post_date, false)) {
            $output = $before.$the_date.$after;
        } else {
            $day_diff = (gmdate('z', current_time('timestamp')) - mysql2date('z', $post->post_date, false));
            if($day_diff < 0) { $day_diff = 32; }
            if($day_diff == 0) {
                $output = $before.__('Сегодня', 'wp-relativedate').$after;
            } elseif($day_diff == 1) {
                $output = $before. __('Вчера', 'wp-relativedate').$after;
            } elseif ($day_diff < 7) {
                if($display_ago_only) {
                    $output = $before.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).$after;
                } else {
                    $output = $before.$the_date.' ('.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).')'.$after;
                }
            } elseif ($day_diff < 31) {
                if($display_ago_only) {
                    $output = $before.sprintf(_n('', '', ceil($day_diff/7), 'wp-relativedate'), number_format_i18n(ceil($day_diff/7))).$after;
                } else {
                    $output = $before.$the_date.' ('.sprintf(_n('', '', ceil($day_diff/7), 'wp-relativedate'), number_format_i18n(ceil($day_diff/7))).')'.$after;
                }
            } else {
                $output = $before.$the_date.$after;
            }
        }
        if($display) {
            echo $output;
        } else {
            return $output;
        }
    }
    


  • Skates are removed in two locations

        } elseif ($day_diff < 7) {
            ...
            } else {
    /* ---> тут   */ $output = $before.$the_date.' ('.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).')'.$after;
            }
        } elseif ($day_diff < 31) {
            ...
            } else {
    /* ---> и тут */ $output = $before.$the_date.' ('.sprintf(_n('', '', ceil($day_diff/7), 'wp-relativedate'), number_format_i18n(ceil($day_diff/7))).')'.$after;
            }
        } else {
    

    Remove both lines and they won't be displayed (and if you've got pages hanging, you need to clear the kees, otherwise you'll fight windmills for a long time)

    It doesn't work very well over time. We need a cricket that can be inserted into a piece. <?php the_time( $d );but it's gonna be pretty loud. You can write your function in a file. functions.php to replace the current topic the_time()

    For example:

    function special_post_time($d = '') {
        global $post;
        $day_diff = (gmdate('z', current_time('timestamp'))
            - mysql2date('z', $post->post_date, false));
        if ($day_diff < 2) {
            the_time($d);
        }
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2