我正在寻找一个函数将“X Hours ago”和“X minutes ago”中显示的日期转换回php中的时间戳,任何人都有这个问题?
答案 0 :(得分:10)
strtotime
已经这样做了:
$timestamp = strtotime("8 hours ago");
有关详细信息,请参阅relative time format specifications。
答案 1 :(得分: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;
}
?>
答案 2 :(得分:1)
$hourago= "-1 hour";
$minago = "-2 minute";
$timestamp = strtotime($hourago.' '.$minago);
echo $timestamp;
或
$hourago= "-1";
$minago = "-2";
$timestamp = strtotime($hourago.' hour '.$minago.' minute');
echo $timestamp;