下面是jQuery的一个非常好的时间前插件,非常类似于他们在这里使用的SO.对我来说问题是它用它来转换时间.
<time class="timeago" datetime="2008-07-17T09:24:17Z">July 17,2008</time>

这将是很好的,除了我在UTC时间戳上存储我的网站时间而不是格式化时间,有没有办法转换这样的东西使用时间戳?我知道在PHP中我可以将我的时间戳转换为这种格式,但在PHP的1页上转换很多次似乎有些过分.我可能是错的,其他人在jquery中这样做但是从真正的时间戳?

此外,我目前在一个网站上用PHP显示“2小时4分钟前”,但是你最好使用javascript来代替PHP吗?

/*
 * timeago: a jQuery plugin,version: 0.8.1 (2010-01-04)
 * @requires jQuery v1.2.3 or later
 *
 * Timeago is a jQuery plugin that makes it easy to support automatically
 * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
 *
 * For usage and examples,visit:
 * http://timeago.yarp.com/
 * copyright (c) 2008-2010,Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
 */
(function($) {
  $.timeago = function(timestamp) {
    if (timestamp instanceof Date) return inWords(timestamp);
    else if (typeof timestamp == "string") return inWords($.timeago.parse(timestamp));
    else return inWords($.timeago.datetime(timestamp));
  };
  var $t = $.timeago;

  $.extend($.timeago,{
    settings: {
      refreshMillis: 60000,allowFuture: false,strings: {
        prefixAgo: null,prefixFromNow: null,suffixAgo: "ago",suffixFromNow: "from Now",ago: null,// DEPRECATED,use suffixAgo
        fromNow: null,use suffixFromNow
        seconds: "less than a minute",minute: "about a minute",minutes: "%d minutes",hour: "about an hour",hours: "about %d hours",day: "a day",days: "%d days",month: "about a month",months: "%d months",year: "about a year",years: "%d years"
      }
    },inWords: function(distanceMillis) {
      var $l = this.settings.strings;
      var prefix = $l.prefixAgo;
      var suffix = $l.suffixAgo || $l.ago;
      if (this.settings.allowFuture) {
        if (distanceMillis < 0) {
          prefix = $l.prefixFromNow;
          suffix = $l.suffixFromNow || $l.fromNow;
        }
        distanceMillis = Math.abs(distanceMillis);
      }

      var seconds = distanceMillis / 1000;
      var minutes = seconds / 60;
      var hours = minutes / 60;
      var days = hours / 24;
      var years = days / 365;

      var words = seconds < 45 && substitute($l.seconds,Math.round(seconds)) ||
        seconds < 90 && substitute($l.minute,1) ||
        minutes < 45 && substitute($l.minutes,Math.round(minutes)) ||
        minutes < 90 && substitute($l.hour,1) ||
        hours < 24 && substitute($l.hours,Math.round(hours)) ||
        hours < 48 && substitute($l.day,1) ||
        days < 30 && substitute($l.days,Math.floor(days)) ||
        days < 60 && substitute($l.month,1) ||
        days < 365 && substitute($l.months,Math.floor(days / 30)) ||
        years < 2 && substitute($l.year,1) ||
        substitute($l.years,Math.floor(years));

      return $.trim([prefix,words,suffix].join(" "));
    },parse: function(iso8601) {
      var s = $.trim(iso8601);
      s = s.replace(/-/,"/").replace(/-/,"/");
      s = s.replace(/T/," ").replace(/Z/," UTC");
      s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
      return new Date(s);
    },datetime: function(elem) {
      // jQuery's `is()` doesn't play well with HTML5 in IE
      var isTime = $(elem).get(0).tagName.toLowerCase() == 'time'; // $(elem).is('time');
      var iso8601 = isTime ? $(elem).attr('datetime') : $(elem).attr('title');
      return $t.parse(iso8601);
    }
  });

  $.fn.timeago = function() {
    var self = this;
    self.each(refresh);

    var $s = $t.settings;
    if ($s.refreshMillis > 0) {
      setInterval(function() { self.each(refresh); },$s.refreshMillis);
    }
    return self;
  };

  function refresh() {
    var data = prepareData(this);
    if (!isNaN(data.datetime)) {
      $(this).text(inWords(data.datetime));
    }
    return this;
  }

  function prepareData(element) {
    element = $(element);
    if (element.data("timeago") === undefined) {
      element.data("timeago",{ datetime: $t.datetime(element) });
      var text = $.trim(element.text());
      if (text.length > 0) element.attr("title",text);
    }
    return element.data("timeago");
  }

  function inWords(date) {
    return $t.inWords(distance(date));
  }

  function distance(date) {
    return (new Date().getTime() - date.getTime());
  }

  function substitute(stringOrFunction,value) {
    var string = $.isFunction(stringOrFunction) ? stringOrFunction(value) : stringOrFunction;
    return string.replace(/%d/i,value);
  }

  // fix for IE6 suckage
  document.createElement('abbr');
  document.createElement('time');
})(jQuery);
我有同样的问题.我正在使用从PHP生成的Unix时间戳,因此我决定快速破解并扩展jQuery timeago的解析功能以额外处理时间戳.奇迹般有效.只需在jquery.timeago.js文件中的第79行附近查找Parse函数,并替换为以下内容:
parse: function(iso8601) {
  if ((iso8601 - 0) == iso8601 && iso8601.length > 0) { // Checks if iso8601 is a unix timestamp
    var s = new Date(iso8601);
    if (isNaN(s.getTime())) { // Checks if iso8601 is formatted in milliseconds
      var s = new Date(iso8601 * 1000); //if not,add milliseconds 
    }
    return s;
  }

  var s = $.trim(iso8601);
  s = s.replace(/-/,"/");
  s = s.replace(/T/," UTC");
  s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
  return new Date(s);
},

php – jQuery Time from a timestamp?的更多相关文章

  1. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. 使用layui实现左侧菜单栏及动态操作tab项的方法

    这篇文章主要介绍了使用layui实现左侧菜单栏及动态操作tab项的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. 视频流 – 使用视频工具箱解码iOS 8中的h264

    需要解码h264流并获取像素缓冲区我知道iOS8上的视频工具箱可能1.如何将h264流转换为CMSampleBufferRef?

  4. 在iOS上绘制扭曲的文本

    使用iOS9及更高版本中提供的标准API,如何在绘制文本时实现扭曲效果?

  5. ios – 访问文件属性与访问sqlite记录

    >看到上述结果后,我决定选择attributesOfItemAtPath方法.还有什么我不是考虑传递sqlite?

  6. ios – 如果Element符合给定的协议,则扩展阵列以符合协议

    如果是这样,语法是什么?解决方法Swift4.2在Swift4.2中,我能够使用符合这样的协议的元素扩展数组:

  7. ios – NSTimeInterval到unix时间戳

    我从cmmotionmanager获取CMDeviceMotion对象.CMDeviceMotion的一个属性是时间戳,表示为NSTimeInterval.根据文档,这允许“亚毫秒”时间戳精度.不幸的是,NSTimeInterval是自上次设备启动以来计算的,对以原始形式使用它提出了重大挑战.有没有人有一个工作代码将此NSTimeInterval转换为类似时间戳的Unix?解决方法在将磁力计值与CoreMotion事件进行比较时,我遇到了类似的问题.如果要转换这些NSTimeIntervals,您只需要计

  8. ios – 更改JSQMessagesViewController中的时间戳逻辑

    SOMessaging一天一天怎么样?

  9. 如何从iOS中的CMSampleBufferRef获取相机数据当前捕获的时间戳

    我开发和iOS应用程序,将捕获的相机数据保存到一个文件,我使用捕获CMSampleBufferRef,并将编码为H264格式,帧将使用AVAssetWriter保存到文件.我遵循示例源代码来创建此应用程序:http://www.gdcl.co.uk//2013/02/20/iOS-Video-Encoding.html现在我想获得保存的视频帧的时间戳来创建一个新的电影文件,为此我做了以下事情1)找

  10. ios – 如何在swift中获取2数组的常见元素列表

    (双关语)编辑:,你可以这样做这个实现是丑陋的.

随机推荐

  1. PHP个人网站架设连环讲(一)

    先下一个OmnihttpdProffesinalV2.06,装上就有PHP4beta3可以用了。PHP4给我们带来一个简单的方法,就是使用SESSION(会话)级变量。但是如果不是PHP4又该怎么办?我们可以假设某人在15分钟以内对你的网页的请求都不属于一个新的人次,这样你可以做个计数的过程存在INC里,在每一个页面引用,访客第一次进入时将访问时间送到cookie里。以后每个页面被访问时都检查cookie上次访问时间值。

  2. PHP函数学习之PHP函数点评

    PHP函数使用说明,应用举例,精简点评,希望对您学习php有所帮助

  3. ecshop2.7.3 在php5.4下的各种错误问题处理

    将方法内的函数,分拆为2个部分。这个和gd库没有一点关系,是ecshop程序的问题。会出现这种问题,不外乎就是当前会员的session或者程序对cookie的处理存在漏洞。进过本地测试,includes\modules\integrates\ecshop.php这个整合自身会员的类中没有重写integrate.php中的check_cookie()方法导致,验证cookie时返回的username为空,丢失了登录状态,在ecshop.php中重写了此方法就可以了。把他加到ecshop.php的最后面去就可

  4. NT IIS下用ODBC连接数据库

    $connection=intodbc_connect建立数据库连接,$query_string="查询记录的条件"如:$query_string="select*fromtable"用$cur=intodbc_exec检索数据库,将记录集放入$cur变量中。再用while{$var1=odbc_result;$var2=odbc_result;...}读取odbc_exec()返回的数据集$cur。最后是odbc_close关闭数据库的连接。odbc_result()函数是取当前记录的指定字段值。

  5. PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下

  6. zen_cart实现支付前生成订单的方法

    这篇文章主要介绍了zen_cart实现支付前生成订单的方法,结合实例形式详细分析了zen_cart支付前生成订单的具体步骤与相关实现技巧,需要的朋友可以参考下

  7. Thinkphp5框架实现获取数据库数据到视图的方法

    这篇文章主要介绍了Thinkphp5框架实现获取数据库数据到视图的方法,涉及thinkPHP5数据库配置、读取、模型操作及视图调用相关操作技巧,需要的朋友可以参考下

  8. PHP+jquery+CSS制作头像登录窗(仿QQ登陆)

    本篇文章介绍了PHP结合jQ和CSS制作头像登录窗(仿QQ登陆),实现了类似QQ的登陆界面,很有参考价值,有需要的朋友可以了解一下。

  9. 基于win2003虚拟机中apache服务器的访问

    下面小编就为大家带来一篇基于win2003虚拟机中apache服务器的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. Yii2中组件的注册与创建方法

    这篇文章主要介绍了Yii2之组件的注册与创建的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

返回
顶部