• <small id='iphwN'></small><noframes id='iphwN'>

      <bdo id='iphwN'></bdo><ul id='iphwN'></ul>

      <legend id='iphwN'><style id='iphwN'><dir id='iphwN'><q id='iphwN'></q></dir></style></legend>
      1. <tfoot id='iphwN'></tfoot>
        <i id='iphwN'><tr id='iphwN'><dt id='iphwN'><q id='iphwN'><span id='iphwN'><b id='iphwN'><form id='iphwN'><ins id='iphwN'></ins><ul id='iphwN'></ul><sub id='iphwN'></sub></form><legend id='iphwN'></legend><bdo id='iphwN'><pre id='iphwN'><center id='iphwN'></center></pre></bdo></b><th id='iphwN'></th></span></q></dt></tr></i><div id='iphwN'><tfoot id='iphwN'></tfoot><dl id='iphwN'><fieldset id='iphwN'></fieldset></dl></div>

        在 PHP 中将时间戳转换为时间前,例如 1 天前、2 天前......

        时间:2024-04-13

        <tfoot id='ZNaDK'></tfoot>

      2. <i id='ZNaDK'><tr id='ZNaDK'><dt id='ZNaDK'><q id='ZNaDK'><span id='ZNaDK'><b id='ZNaDK'><form id='ZNaDK'><ins id='ZNaDK'></ins><ul id='ZNaDK'></ul><sub id='ZNaDK'></sub></form><legend id='ZNaDK'></legend><bdo id='ZNaDK'><pre id='ZNaDK'><center id='ZNaDK'></center></pre></bdo></b><th id='ZNaDK'></th></span></q></dt></tr></i><div id='ZNaDK'><tfoot id='ZNaDK'></tfoot><dl id='ZNaDK'><fieldset id='ZNaDK'></fieldset></dl></div>
        <legend id='ZNaDK'><style id='ZNaDK'><dir id='ZNaDK'><q id='ZNaDK'></q></dir></style></legend>

          <tbody id='ZNaDK'></tbody>

      3. <small id='ZNaDK'></small><noframes id='ZNaDK'>

              • <bdo id='ZNaDK'></bdo><ul id='ZNaDK'></ul>
                • 本文介绍了在 PHP 中将时间戳转换为时间前,例如 1 天前、2 天前......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试转换格式为 2009-09-12 20:57:19 的时间戳,并使用 PHP 将其转换为类似于 3 minutes ago 的格式.

                  I am trying to convert a timestamp of the format 2009-09-12 20:57:19 and turn it into something like 3 minutes ago with PHP.

                  我找到了一个有用的脚本来执行此操作,但我认为它正在寻找一种不同的格式来用作时间变量.我要修改以使用这种格式的脚本是:

                  I found a useful script to do this, but I think it's looking for a different format to be used as the time variable. The script I'm wanting to modify to work with this format is:

                  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;
                      }
                  

                  我认为在前几行中,脚本试图做一些看起来像这样的事情(不同的日期格式数学):

                  I think on those first few lines the script is trying to do something that looks like this (different date format math):

                  $dif = 1252809479 - 2009-09-12 20:57:19;
                  

                  如何将我的时间戳转换为那种(unix?)格式?

                  How would I go about converting my timestamp into that (unix?) format?

                  推荐答案

                  使用示例:

                  echo time_elapsed_string('2013-05-01 00:22:35');
                  echo time_elapsed_string('@1367367755'); # timestamp input
                  echo time_elapsed_string('2013-05-01 00:22:35', true);
                  

                  输入可以是任何支持的日期和时间格式..p>

                  输出:

                  4 months ago
                  4 months ago
                  4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
                  

                  功能:

                  function time_elapsed_string($datetime, $full = false) {
                      $now = new DateTime;
                      $ago = new DateTime($datetime);
                      $diff = $now->diff($ago);
                  
                      $diff->w = floor($diff->d / 7);
                      $diff->d -= $diff->w * 7;
                  
                      $string = array(
                          'y' => 'year',
                          'm' => 'month',
                          'w' => 'week',
                          'd' => 'day',
                          'h' => 'hour',
                          'i' => 'minute',
                          's' => 'second',
                      );
                      foreach ($string as $k => &$v) {
                          if ($diff->$k) {
                              $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
                          } else {
                              unset($string[$k]);
                          }
                      }
                  
                      if (!$full) $string = array_slice($string, 0, 1);
                      return $string ? implode(', ', $string) . ' ago' : 'just now';
                  }
                  

                  这篇关于在 PHP 中将时间戳转换为时间前,例如 1 天前、2 天前......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在laravel中将对象转换为数组 下一篇:PHP如何查找自日期时间以来经过的时间?

                  相关文章

                  <tfoot id='zQmHG'></tfoot>

                    <bdo id='zQmHG'></bdo><ul id='zQmHG'></ul>
                  1. <small id='zQmHG'></small><noframes id='zQmHG'>

                    <i id='zQmHG'><tr id='zQmHG'><dt id='zQmHG'><q id='zQmHG'><span id='zQmHG'><b id='zQmHG'><form id='zQmHG'><ins id='zQmHG'></ins><ul id='zQmHG'></ul><sub id='zQmHG'></sub></form><legend id='zQmHG'></legend><bdo id='zQmHG'><pre id='zQmHG'><center id='zQmHG'></center></pre></bdo></b><th id='zQmHG'></th></span></q></dt></tr></i><div id='zQmHG'><tfoot id='zQmHG'></tfoot><dl id='zQmHG'><fieldset id='zQmHG'></fieldset></dl></div>

                    1. <legend id='zQmHG'><style id='zQmHG'><dir id='zQmHG'><q id='zQmHG'></q></dir></style></legend>