Symfony 将createdAt属性显示为“0”;已在x小时x分钟前上载”; 投票:{{imagevoces}} 点击: 上传:{createdAt|date} OP:{{imageOwner}

Symfony 将createdAt属性显示为“0”;已在x小时x分钟前上载”; 投票:{{imagevoces}} 点击: 上传:{createdAt|date} OP:{{imageOwner},symfony,twig,Symfony,Twig,{{createdAt | date}}返回类似于“2014年4月15日09:39”的内容 我需要它来显示创建映像的时间是多少小时/分钟前请确保createdAt是unix时间戳格式,然后您可以使用一些基本数学知识 一些链接可以帮助你做到这一点 代码,以防链接断开。 <div class="avatar-details" id="avatar-details{{imageId}}"> <ul class="details-list"> <li

{{createdAt | date}}返回类似于“2014年4月15日09:39”的内容
我需要它来显示创建映像的时间是多少小时/分钟前

请确保createdAt是unix时间戳格式,然后您可以使用一些基本数学知识

一些链接可以帮助你做到这一点
代码,以防链接断开。
<div class="avatar-details" id="avatar-details{{imageId}}">
    <ul class="details-list">
        <li><b>Votes:</b><span id="image_votes{{imageId}}">{{ imageVotes }}</span></li>
        <li><b>Clicks:</b></li>
        <!--<li><b>Last action:</b><span id="last_user_action{{imageId}}"></span></li> -->
        <li><b>Uploaded</b>: {{ createdAt | date}}</li>
        <li><b>OP:</b><br>{{imageOwner}}</li>
    </ul>
</div>
$now=time();
$difference=$now-$time;
$tense=“ago”;
对于($j=0;$difference>=$length[$j]&&$j

function\u ago($tm,$rcs=0){
$cur_tm=time();$dif=$cur_tm-$tm;
$pds=数组('second'、'minute'、'hour'、'day'、'week'、'month'、'year'、'decade');
$lngh=阵列(1,6036008640060480026308031570560315705600);
对于($v=sizeof($lngh)-1;($v>=0)和($no=$dif/$lngh[$v])=1)和($cur_tm-$\u tm)>0))$x.=时间($\u tm);
返回$x;
}
或 代码副本(以防链接失效

function _ago($tm,$rcs = 0) {
   $cur_tm = time(); $dif = $cur_tm-$tm;
   $pds = array('second','minute','hour','day','week','month','year','decade');
   $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
   for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);

   $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
   if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
   return $x;
}

Like?是的,不是“像以前一样”,但这完全是胡说八道,对于一个简单的输出来说,这是很多代码。@user3531149我不同意你的观点。但是,与编写自己的文件相比,谷歌、复制和粘贴要花多长时间?这与所需的工作无关。通常使用这种格式来处理日期的输出{{createdAt | date(“dostuff”)}所以我很惊讶需要这么多行代码才能实现这一点。
function _ago($tm,$rcs = 0) {
   $cur_tm = time(); $dif = $cur_tm-$tm;
   $pds = array('second','minute','hour','day','week','month','year','decade');
   $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
   for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);

   $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
   if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
   return $x;
}
<?php
/* Works out the time since the entry post, takes a an argument in unix time (seconds) 
*/
static public function Timesince($original) {
    // array of time period chunks
    $chunks = array(
    array(60 * 60 * 24 * 365 , 'year'),
    array(60 * 60 * 24 * 30 , 'month'),
    array(60 * 60 * 24 * 7, 'week'),
    array(60 * 60 * 24 , 'day'),
    array(60 * 60 , 'hour'),
    array(60 , 'min'),
    array(1 , 'sec'),
    );

    $today = time(); /* Current unix time  */
    $since = $today - $original;

    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {

    $seconds = $chunks[$i][0];
    $name = $chunks[$i][1];

    // finding the biggest chunk (if the chunk fits, break)
    if (($count = floor($since / $seconds)) != 0) {
        break;
    }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    if ($i + 1 < $j) {
    // now getting the second item
    $seconds2 = $chunks[$i + 1][0];
    $name2 = $chunks[$i + 1][1];

    // add second item if its greater than 0
    if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
        $print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";
    }
    }
    return $print;
}