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

发布于 2024-12-05 12:07:45 字数 62 浏览 0 评论 0原文

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

I'm looking for a function to convert dates presented in "X Hours ago" and "X minutes ago" back into a timestamp in php, anyone have a soulution to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请持续率性 2024-12-12 12:07:45

strtotime 已经做到了这一点:

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

请参阅相对时间格式规范了解更多信息。

strtotime already does this:

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

See relative time format specifications for more info.

南城旧梦 2024-12-12 12:07:45

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

}
?>

I helped someone on stackoverflow to write a function that does the reverse of this, here is the code, i'm sure if you deconstruct and reverse it, you will have your answer:

<?
$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;

}
?>
淡写薰衣草的香 2024-12-12 12:07:45
$hourago= "-1 hour";
$minago = "-2 minute";

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

或者

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

$timestamp = strtotime($hourago.' hour '.$minago.' minute');
echo $timestamp;
$hourago= "-1 hour";
$minago = "-2 minute";

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

or

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

$timestamp = strtotime($hourago.' hour '.$minago.' minute');
echo $timestamp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文