如何在Magento fishpig中显示WordPress帖子的时间(例如:1天前)?
<?php $posts = $this->getPosts() ?>
<?php if (count($posts) > 0): ?>
<div class="block block-blog block-recent-posts">
<?php if ($title = $this->getTitle()): ?>
<div class="block-title">
<strong><span><?php echo $this->__($title) ?></span></strong>
</div>
<?php endif; ?>
<div class="block-content">
<ul id="<?php echo $this->getListId() ?>">
<?php foreach($posts as $post): ?>
<li class="item">
<a href="<?php echo $post->getPermalink() ?>" title="<?php echo $this->htmlEscape($post->getPostTitle()) ?>"><?php echo $this->htmlEscape($post->getPostTitle()) ?></a>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script>
</div>
</div>
<?php endif; ?>
答案 0 :(得分:1)
要获得你在foreach循环中需要做的帖子的日期:
<?php echo $post->getPostDate() ?>
如果你想要现在和postdate之间的差异,我。即 1天前您可能需要编写自己的功能。 Wordpress有自己的函数human_time_diff,用于Magento中不存在的函数。
答案 1 :(得分:-1)
对于人类时差使用:
function get_date_diff( $time1, $time2, $precision = 2 ) {
// If not numeric then convert timestamps
if( !is_int( $time1 ) ) {
$time1 = strtotime( $time1 );
}
if( !is_int( $time2 ) ) {
$time2 = strtotime( $time2 );
}
// If time1 > time2 then swap the 2 values
if( $time1 > $time2 ) {
list( $time1, $time2 ) = array( $time2, $time1 );
}
// Set up intervals and diffs arrays
$intervals = array( 'year', 'month', 'day', 'hour', 'minute', 'second' );
$diffs = array();
foreach( $intervals as $interval ) {
// Create temp time from time1 and interval
$ttime = strtotime( '+1 ' . $interval, $time1 );
// Set initial values
$add = 1;
$looped = 0;
// Loop until temp time is smaller than time2
while ( $time2 >= $ttime ) {
// Create new temp time from time1 and interval
$add++;
$ttime = strtotime( "+" . $add . " " . $interval, $time1 );
$looped++;
}
$time1 = strtotime( "+" . $looped . " " . $interval, $time1 );
$diffs[ $interval ] = $looped;
}
$count = 0;
$times = array();
foreach( $diffs as $interval => $value ) {
// Break if we have needed precission
if( $count >= $precision ) {
break;
}
// Add value and interval if value is bigger than 0
if( $value > 0 ) {
if( $value != 1 ){
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode( ", ", $times );
}
打印时间戳(POST): $ t1 = $ post-&gt; getPostDate(c);
打印时间戳(当前时间):$ t2 = Mage :: getModel('core / date') - &gt; date('c');
<强> USAGE:强>
var_dump(get_date_diff($ t,$ t2,1)); //字符串'1个月'(长度= 7)
var_dump(get_date_diff($ t,$ t2,2)); //字符串'1个月,4天'(长度= 15)
var_dump(get_date_diff($ t,$ t2,3)); //字符串'1个月,4天,5个小时'(长度= 24)