在 Magento Fishpig 中获得 Post 时间
Get Post time in Magento Fishpig
如何在 Magento fishpig 中显示 WordPress post(例如: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; ?>
要获取 post 的日期,您需要在 foreach 循环中执行以下操作:
<?php echo $post->getPostDate() ?>
如果你想要现在和post日期之间的区别,我。 e. 1 天前 您可能必须编写自己的函数。对于 Magento 中不存在的功能,Wordpress 有自己的功能 human_time_diff。
对于人类时差使用:
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 );
}
To print Timestamp(POST) : $t1 = $post->getPostDate(c);
To print Timestamp(Current Time) : $t2 = Mage::getModel('core/date')->date('c');
用法:
var_dump( get_date_diff( $t, $t2, 1 ) ); // string '1 month'(length=7)
var_dump( get_date_diff( $t, $t2, 2 ) ); // string '1 month, 4 days' (length=15)
var_dump( get_date_diff( $t, $t2, 3 ) ); //string '1 month, 4 days, 5 hours' (length=24)
如何在 Magento fishpig 中显示 WordPress post(例如: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; ?>
要获取 post 的日期,您需要在 foreach 循环中执行以下操作:
<?php echo $post->getPostDate() ?>
如果你想要现在和post日期之间的区别,我。 e. 1 天前 您可能必须编写自己的函数。对于 Magento 中不存在的功能,Wordpress 有自己的功能 human_time_diff。
对于人类时差使用:
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 );
}
To print Timestamp(POST) : $t1 = $post->getPostDate(c);
To print Timestamp(Current Time) : $t2 = Mage::getModel('core/date')->date('c');
用法:
var_dump( get_date_diff( $t, $t2, 1 ) ); // string '1 month'(length=7)
var_dump( get_date_diff( $t, $t2, 2 ) ); // string '1 month, 4 days' (length=15)
var_dump( get_date_diff( $t, $t2, 3 ) ); //string '1 month, 4 days, 5 hours' (length=24)