{"id":38,"date":"2020-11-21T01:09:03","date_gmt":"2020-11-20T17:09:03","guid":{"rendered":"https:\/\/blog.51time.top\/?p=38"},"modified":"2022-01-14T16:47:06","modified_gmt":"2022-01-14T08:47:06","slug":"time_ago_in_words","status":"publish","type":"post","link":"https:\/\/blog.51time.top\/index.php\/2020\/11\/21\/time_ago_in_words\/","title":{"rendered":"\u6539\u5199time_ago_in_words\u65b9\u6cd5"},"content":{"rendered":"<h2>\u95ee\u9898<\/h2>\n<p>rails\u4e2d\u7684time_ago_in_words\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u793a\u8ddd\u79bb\u5f53\u524d\u65f6\u95f4\u591a\u4e45\u4e4b\u524d\u7684\u65f6\u95f4\u3002\u6bd4\u5982\u53d1\u8868\u4e00\u7bc7\u6587\u7ae0\uff0c\u4ee3\u7801\u53ef\u4ee5\u8fd9\u6837\u5199 \uff1a<\/p>\n<pre><code class=\"language-ruby\">  post at <%= time_ago_in_words(@article.created_at) %> ago<\/code><\/pre>\n<p>\u9875\u9762\u4e2d\u663e\u793a\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n`\u201cpost at 5 days ago\u201d`<br \/>\n\u5982\u679c\u8981\u6539\u6210\u4e2d\u6587\u6587\u5b57\uff0c\u9700\u8981\u5bf9\u8be5\u65b9\u6cd5\u91cd\u5199\u3002<\/p>\n<h2>\u89e3\u51b3<\/h2>\n<p>\u67e5\u627erails\u6587\u6863\uff0c\u53ef\u4ee5\u53d1\u73b0\u8fd9\u4e2a\u65b9\u6cd5\u662f\u8c03\u7528\u4e86\u5982\u4e0b\u8fd9\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u7684\uff1a<\/p>\n<pre><code class=\"language-ruby\">  def time_ago_in_words(from_time, include_seconds = false)\n     distance_of_time_in_words(from_time, Time.now, include_seconds)\nend<\/code><\/pre>\n<p>\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u91cd\u5199 distance_of_time_in_words \u65b9\u6cd5\u5373\u53ef\u3002\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-ruby\">def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)\n    from_time = from_time.to_time if from_time.respond_to?(:to_time)\n    to_time = to_time.to_time if to_time.respond_to?(:to_time)\n    distance_in_minutes = (((to_time - from_time).abs)\/60).round\n    distance_in_seconds = ((to_time - from_time).abs).round\n\n    case distance_in_minutes\n        when 0..1\n            return (distance_in_minutes == 0) ? '\u4e0d\u5230 1 \u5206\u949f' : '1 \u5206\u949f' unless include_seconds\n        case distance_in_seconds\n            when 0..4   then '\u4e0d\u5230 5 \u79d2'\n            when 5..9   then '\u4e0d\u5230 10 \u79d2'\n            when 10..19 then '\u4e0d\u5230 20 \u79d2'\n            when 20..39 then '\u534a\u5206\u949f'\n            when 40..59 then '\u4e0d\u5230 1 \u5206\u949f'\n            else             '1 \u5206\u949f'\n        end\n\n        when 2..44           then \"#{distance_in_minutes} \u5206\u949f\"\n        when 45..89          then '\u5927\u7ea6 1 \u5c0f\u65f6'\n        when 90..1439        then \"\u5927\u7ea6 #{(distance_in_minutes.to_f \/ 60.0).round} \u5c0f\u65f6\"\n        when 1440..2879      then '1 \u5929'\n        when 2880..43199     then \"#{(distance_in_minutes \/ 1440).round} \u5929\"\n        when 43200..86399    then '\u5927\u7ea6 1 \u4e2a\u6708'\n        when 86400..525599   then \"#{(distance_in_minutes \/ 43200).round} \u4e2a\u6708\"\n        when 525600..1051199 then '\u5927\u7ea6 1 \u5e74'\n    else                      \"#{(distance_in_minutes \/ 525600).round} \u5e74\"\n   end\nend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898 rails\u4e2d\u7684time_ago_in_words\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u793a\u8ddd\u79bb\u5f53\u524d\u65f6\u95f4\u591a\u4e45\u4e4b\u524d\u7684\u65f6\u95f4\u3002\u6bd4\u5982\u53d1\u8868\u4e00\u7bc7\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/posts\/38"}],"collection":[{"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":1,"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.51time.top\/index.php\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}