常规的我们发布的WordPress文章的时间格式按照正常的日期,有些时候我们看到有的网站是几天前的这样比较个性化的格式,那要如何实现呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function format_date($time) { global $options, $post; $p_id = isset($post->ID) ? $post->ID : 0; $q_id = get_queried_object_id(); $single = $p_id == $q_id && is_single(); if (isset($options['time_format']) && $options['time_format'] == '0') { return date(get_option('date_format') . ($single ? ' ' . get_option('time_format') : ''), $time); } $t = current_time('timestamp') - $time; $f = array( '86400' => '天', '3600' => '小时', '60' => '分钟', '1' => '秒' ); if ($t == 0) { return '1秒前'; } else if ($t >= 604800 || $t < 0) { return date(get_option('date_format') . ($single ? ' ' . get_option('time_format') : ''), $time); } else { foreach ($f as $k => $v) { if (0 != $c = floor($t / (int)$k)) { return $c . $v . '前'; } } } } add_filter('the_time','format_date'); |
这样,我们的时间格式就自动几天前的格式。
声明:本站发布的所有资源均来自于互联网,所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
如侵犯到您的权益,请及时通知我们,我们会及时处理。邮箱:505289534@qq.com
评论(0)