使用jquery格式化多个php日期

使用jquery格式化多个php日期,php,jquery,date,mysqli,format,Php,Jquery,Date,Mysqli,Format,我在PHPdate('Y-m-dh:I:s')中有格式为yyy-mm-dd hh:mm:ss的日期,以适合我的mysqli数据库。我希望它采用以下格式: 1) /*x hours ago*/ <span class="time">2014-02-24 10:38:19</span> 2) /*Yesterday*/ <span class="time">2014-02-23 16:38:19</span> 3) /*Feb 22nd*/ <

我在PHP
date('Y-m-dh:I:s')
中有格式为
yyy-mm-dd hh:mm:ss
的日期,以适合我的mysqli数据库。我希望它采用以下格式:

1) /*x hours ago*/ <span class="time">2014-02-24 10:38:19</span>

2) /*Yesterday*/ <span class="time">2014-02-23 16:38:19</span>

3) /*Feb 22nd*/ <span class="time">2014-02-22 16:38:19</span>

4) /*2013 Feb 22nd 16:38*/ <span class="time">2013-02-22 16:38:19</span>

目前,上面的代码只会格式化第一次
时间
而不是全部?

编辑:

如果您愿意自己动手,我可以对您现有的代码提出以下要点:

尝试在代码中使用jQuery的
$.each()
函数和
$(this)
,在每个标记上循环并一次处理一个标记。当您执行
$('.time')
并且存在多个匹配项时,jQuery将返回一个对象数组,该数组无法以您尝试使用它的方式在if语句中轻松使用

这个稍微修改过的代码版本可以实现以下功能:

var today = "<?php echo $today; ?>",
    yesterday = "<?php echo $yesterday ?>",
    year = "<?php echo $year ?>";

$('.time').each(function() {
   $this = $(this);
   if (today === $.format.date($this.html(), "yyyy-MM-dd") || yesterday === $.format.date($this.html(), "yyyy-MM-dd")) {
      $this.html($.format.prettyDate($this.html()));
   } else if (year !== $.format.date($this.html(), "yyyy")) {
       $this.html($.format.date($this.html(), "MMM D - yyyy"));
   } else {
       $this.html($.format.date($this.html(), "MMM D"));
   }
});
将产生以下内容:

<p><abbr class="time" title="2012-02-22 20:20:20">2 years ago</abbr></p>
<p><abbr class="time" title="2014-01-22 20:20:20">a month ago</abbr></p>
<p><abbr class="time" title="2014-02-24 15:20:20">28 minutes ago</abbr></p>

这很有效

$( '.time' ).each(function( index ) {
if ("<?php echo $date ?>" === $.format.date($(this).html(), "yyyy-MM-dd") || "<?php echo $yesterday ?>" === $.format.date($('.time').html(), "yyyy-MM-dd")) {
    $(this).html($.format.prettyDate($(this).html()));
} else if ("<?php echo $year ?>" !== $.format.date($(this).html(), "yyyy")) {
    $(this).html($.format.date($(this).html(), "MMM D - yyyy"));
} else {
    $(this).html($.format.date($(this).html(), "MMM D"));
}
});
$('.time')。每个(函数(索引){
如果(“===$.format.date($(this.html(),“yyyyy-MM-dd”)| |===$.format.date($('.time').html(),“yyyy-MM-dd”)){
$(this.html($.format.prettyDate($(this.html()));
}else if(“!==$.format.date($(this.html(),“yyyy”)){
$(this.html($.format.date($(this.html(),“MMM D-yyyy”));
}否则{
$(this.html($.format.date($(this.html(),“mmmd”));
}
});

为了澄清这一点,您需要将日期(如
2014-02-24 00:00:00
)转换为
X小时前的日期
?我基本上是为每种类型的声明寻找一个默认时刻的
。js实际上打印的是“一天前”而不是“昨天”。但这也可以根据这个问题/答案进行定制,使用moment.js很容易随时间变化进行操作,但我认为他可能对java脚本感兴趣so@KarthickKumarGanesh如果你想自己写,那当然是他们的选择。但我发现,当一个拥有178名贡献者(到目前为止)的开源社区已经维护并解决了各种各样的边缘案例时,这几乎是不值得的,因为单凭一个开发人员是不可能发现的。我随时都会使用moment.js,而不是重新发明轮子。另外,这个问题显然已经使用了php和jquery,“java脚本”会比矩.js更受欢迎吗?矩.js本身就是javascript。为什么这与我在3小时前添加到答案中的代码片段几乎相同。而这段代码实际上并不完整,“昨天”与$(“.time”)的比较也需要是$(this)。或者更好的是,当您需要像这里一样调用$(this)7次时,最好将结果缓存在一个变量中,如so$this=$(this),然后只使用$this。为什么?->到最后一位?我们为什么要使用
$this=$(this)
?因为
$()
是一个函数,当您执行
$(this)
时,您实际上是在
this
对象上调用一个函数来“jquery”它,这涉及到一些处理工作(不是很多,但仍然是工作)。因此,您可以将该工作的结果保存到一个局部变量中,从而只对每个
这个
对象执行一次“jqueryization”,而不是对每个
.time
标记调用$(this)7次。
var today = "<?php echo $today; ?>",
    yesterday = "<?php echo $yesterday ?>",
    year = "<?php echo $year ?>";

$('.time').each(function() {
   $this = $(this);
   if (today === $.format.date($this.html(), "yyyy-MM-dd") || yesterday === $.format.date($this.html(), "yyyy-MM-dd")) {
      $this.html($.format.prettyDate($this.html()));
   } else if (year !== $.format.date($this.html(), "yyyy")) {
       $this.html($.format.date($this.html(), "MMM D - yyyy"));
   } else {
       $this.html($.format.date($this.html(), "MMM D"));
   }
});
<p><abbr class="time" title="<?php echo date('Y-m-d H:i:s'); ?>"><?php date('M dd'); ?></abbr></p>
<p><abbr class="time" title="2012-02-22 20:20:20">Feb 22</abbr></p>
<p><abbr class="time" title="2014-01-22 20:20:20">Jan 2</abbr></p>
<p><abbr class="time" title="2014-02-24 15:20:20">Feb 24</abbr></p>
$('abbr.time').each(function(){
   $this = $(this);
   $this.text(moment($this.attr('title')).fromNow());
});
<p><abbr class="time" title="2012-02-22 20:20:20">2 years ago</abbr></p>
<p><abbr class="time" title="2014-01-22 20:20:20">a month ago</abbr></p>
<p><abbr class="time" title="2014-02-24 15:20:20">28 minutes ago</abbr></p>
$( '.time' ).each(function( index ) {
if ("<?php echo $date ?>" === $.format.date($(this).html(), "yyyy-MM-dd") || "<?php echo $yesterday ?>" === $.format.date($('.time').html(), "yyyy-MM-dd")) {
    $(this).html($.format.prettyDate($(this).html()));
} else if ("<?php echo $year ?>" !== $.format.date($(this).html(), "yyyy")) {
    $(this).html($.format.date($(this).html(), "MMM D - yyyy"));
} else {
    $(this).html($.format.date($(this).html(), "MMM D"));
}
});