【问题标题】:Convert x hours x minutes ago back into unix timestamp将 x 小时 x 分钟前转换回 unix 时间戳
【发布时间】:2011-09-16 16:58:34
【问题描述】:

我正在寻找将“X 小时前”和“X 分钟前”中显示的日期转换回 php 中的时间戳的函数,有人对此有意见吗?

【问题讨论】:

  • 为什么这个问题被标记为 MySQL?
  • 因为我想在转换后插入到MySQL中,不好意思没提。
  • 我根本不明白这有什么关系。如果你愿意,可以打印出来用强力胶粘在头上,但这与问题无关。

标签: php unix-timestamp


【解决方案1】:

strtotime 已经这样做了:

$timestamp = strtotime("8 hours ago");

请参阅relative time format specifications 了解更多信息。

【讨论】:

  • 非常感谢!我坚持我已经尝试过了,但一定是出了什么问题。现在工作得很好。
【解决方案2】:

我帮助stackoverflow上的某个人编写了一个与此相反的函数,这是代码,我相信如果你解构并反转它,你会得到答案:

<?
$unix_time = 6734;
echo howLongAgo($unix_time);

function howLongAgo($time_difference){

// Swtich logic based on the time difference passed to this function, sets the english string and what number the difference needs to be divided by
    switch($time_difference){
         case ($time_difference < 60):
              $string = " second";
              break;
         case ($time_difference >= 60 && $time_difference < 3600):
              $string = " minute";
              $divider = 60;
              break;
         case ($time_difference >= 3600 && $time_difference < 86400):
              $string = " hour";
              $divider = 3600;
              break;
         case ($time_difference >= 86400 && $time_difference < 2629743):
              $string = " day";
              $divider = 86400;
              break;
         case ($time_difference >= 2629743 && $time_difference < 31556926):
              $string = " month";
              $divider = 2629743;
              break;
         case ($time_difference >= 31556926):
              $string = " year";
              $divider = 31556926;
              break;
    }

// If a divider value is set during the switch, use it to get the actual difference
if($divider){$diff = round($time_difference / $divider);}else{$diff = round($time_difference);}
// If the difference does not equal 1, pluralize the final result EG: hours, minutes, seconds
if($diff != 1){$pluralize="s";}
// Concatenate all variables together and return them
$final =  $diff . $string . $pluralize . " ago";
return $final;

}
?>

【讨论】:

  • 有点矫枉过正。此外,这有一个微妙的错误,它不能正确解释 DST(间隔是硬编码的)。
  • 并且没有回答这个问题。这两个任务实际上是完全不同的。
  • 对不起,只是想帮忙
  • 你应该已经看到了这些家伙的原始函数,它是嵌套在 if 语句中的 if 语句,我所做的只是按照他的逻辑编写 switch/case。这是什么函数,我让他知道PHP中内置了一个转换时间的函数。
【解决方案3】:
$hourago= "-1 hour";
$minago = "-2 minute";

$timestamp = strtotime($hourago.' '.$minago);
echo $timestamp;

$hourago= "-1";
$minago = "-2";

$timestamp = strtotime($hourago.' hour '.$minago.' minute');
echo $timestamp;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2021-12-18
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多