Php 将x小时x分钟前的时间转换回unix时间戳

Php 将x小时x分钟前的时间转换回unix时间戳,php,unix-timestamp,Php,Unix Timestamp,我正在寻找一个函数,将“X小时前”和“X分钟前”中显示的日期转换回php中的时间戳,有人对此有解决方案吗 $hourago= "-1 hour"; $minago = "-2 minute"; $timestamp = strtotime($hourago.' '.$minago); echo $timestamp; 或 我帮助stackoverflow上的某个人编写了一个函数,该函数与此相反,下面是代码,我相信如果您解构并反转它,您将得到您的答案: <? $unix_time = 6

我正在寻找一个函数,将“X小时前”和“X分钟前”中显示的日期转换回php中的时间戳,有人对此有解决方案吗

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

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


我帮助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;

}
?>

已经做到了这一点:

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

有关更多信息,请参阅。

为什么这个问题被标记为MySQL?因为我想在转换后将其插入MySQL,很抱歉没有提到这一点。我不知道这有什么关系。如果你愿意,你可以把它打印出来,然后用强力胶粘在你的头上,但这与问题无关。有点过分了。此外,它还有一个微妙的缺陷,它不能正确地解释DST(间隔是硬编码的),并且不能回答这个问题。这两个任务实际上是完全不同的。对不起,伙计们,我只是想帮你们看看伙计们的原始函数,它是嵌套在if语句中的if语句,我所做的只是获取他的逻辑并编写switch/case。什么函数可以做到这一点,我会让他知道PHP中内置了一个转换时间的函数!我想我已经试过了,但一定是出了什么问题。现在一切都很好。
$timestamp = strtotime("8 hours ago");