php Unix 时间戳到秒、分、小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3267896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:11:03  来源:igfitidea点击:

Unix timestamp to seconds, minutes, hours

phpmysql

提问by Damjan

I need to somehow take a unix timestamp and output it like below

我需要以某种方式获取一个 unix 时间戳并像下面这样输出它

Can this be done with MySQL? Or php

这可以用 MySQL 完成吗?或者 php

Mike                             7s ago
Jim                              44s ago
John                             59s ago
Amanda                           1m ago
Ryan                             1m ago
Sarah                            1m ago
Tom                              2m ago
Pamela                           2m ago
Ruben                            3m ago
Pamela                           5h ago

As you can guess i only wanna print the minute, not minutes and seconds(1m 3s ago)

你可以猜到我只想打印分钟,而不是分钟和秒(1m 3s ago)

What should I look into?

我应该看什么?

回答by TCCV

Yes it can be done. See related post

是的,它可以做到。见相关帖子

$before // this is a UNIX timestamp from some time in the past, maybe loaded from mysql
$now = time()

$diff = $now - $before;
if( 1 > $diff ){
   exit('Target Event Already Passed (or is passing this very instant)');
} else {
   $w = $diff / 86400 / 7;
   $d = $diff / 86400 % 7;
   $h = $diff / 3600 % 24;
   $m = $diff / 60 % 60; 
   $s = $diff % 60;

   return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
}

回答by Powerlord

PHP 5.3 and newer have DateTimeobjects that you can construct with data coming back from a database. These DateTimeobjects have a diffmethod to get the difference between two dates as a DateIntervalobject, which you can then format.

PHP 5.3 和更新版本具有DateTime您可以使用从数据库返回的数据构造的对象。这些DateTime对象有一个diff方法可以将两个日期之间的差异作为一个DateInterval对象,然后您可以format.

Edit: corrected subto diff.

编辑:更正subdiff.

Edit 2: Two catches with doing it this way:

编辑 2:这样做有两个问题:

  1. DateTime's constructor doesn't appear to take a UNIX timestamp... unless prefixed with an @, like this: $startDate = new DateTime('@' . $timestamp);
  2. You won't know what the largest unit is without manually checking them. To get an individual field, you still need to use format, but with just a single code... Something like $years = $dateDiff->format('y');
  1. DateTime的构造函数似乎不采用 UNIX 时间戳...除非以 @ 为前缀,如下所示: $startDate = new DateTime('@' . $timestamp);
  2. 如果不手动检查它们,您将不知道最大的单位是什么。要获得单个字段,您仍然需要使用格式,但只需一个代码......就像$years = $dateDiff->format('y');

回答by CoreCoder

function sECONDS_TO_DHMS($seconds)
{

    $days = floor($seconds/86400);
    $hrs = floor($seconds / 3600);
    $mins = intval(($seconds / 60) % 60); 
    $sec = intval($seconds % 60);

    if($days>0){
        //echo $days;exit;
        $hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
        $hours = $hrs-($days*24);
        $return_days = $days." Days ";
        $hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
    }else{
        $return_days="";
        $hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
    }

    $mins = str_pad($mins,2,'0',STR_PAD_LEFT);
    $sec = str_pad($sec,2,'0',STR_PAD_LEFT);

    return $return_days.$hrs.":".$mins.":".$sec;
}

echo sECONDS_TO_DHMS(2); // Output 00:00:02
echo sECONDS_TO_DHMS(96000); // Output 1 Days 02:40:00

回答by Viper_Sb

PHP's date()function

PHP 的date()函数

as well as time()and some others that are linked in those docs

以及time()和这些文档中链接的其他一些

This can also be done in Mysql with date and timefunctions

这也可以在带有日期和时间函数的Mysql 中完成

回答by bakkal

You can try my Timeagosuggestion here.

你可以在这里试试我的Timeago建议。

It can give outputs like this:

它可以提供如下输出:

You opened this page less than a minute ago. (This will update every minute. Wait for it.)

This page was last modified 11 days ago.

Ryan was born 31 years ago.

您在不到一分钟前打开了此页面。(这将每分钟更新一次。等待它。)

此页面最后修改于11 天前

瑞安出生于31 年前

回答by Toby Allen

I dont have a mysql server at hand, but a combination of the following commands should get you something like what you want.

我手头没有 mysql 服务器,但是以下命令的组合应该能让你得到你想要的东西。

DATEDIFF

日期差异

AND

DATEFORMAT

日期格式