PHP 将时间戳转换为月和日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2191573/
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 05:26:22  来源:igfitidea点击:

PHP Convert timestamp to months and days

phpdatetimetimestamp

提问by Blackbeard

How can I convert a timestamp difference between 2 dates

如何转换两个日期之间的时间戳差异

$diff = abs(strtotime($date2) - strtotime($date1));

to months and days quantity as desired ouput:

根据需要输出到月数和天数:

$res = 4.05 //-> 4 months and 5 days

回答by Yacoby

Something like this would possibly work. My maths may be a bit off.

像这样的事情可能会奏效。我的数学可能有点差。

$diff = abs(strtotime($date2) - strtotime($date1));

define('DAY',60*60*24, true);
define('MONTH',DAY*30, true);
define('YEAR',DAY*365, true);

$years = floor($diff / (YEAR));
$months = floor(($diff - $years * YEAR) / (MONTH));
$days = floor(($diff - $years * YEAR - $months*MONTH ) / (DAY));

回答by PhoebeB

You can't approach it like that as month length varies - unless you want to make an assumption based on average month length. If you want the correct difference you need to use the actual dates then you can use php date diff - http://www.php.net/manual/en/datetime.diff.php

随着月份长度的变化,您不能像这样接近它 - 除非您想根据平均月份长度做出假设。如果您想要正确的差异,您需要使用实际日期,那么您可以使用 php date diff - http://www.php.net/manual/en/datetime.diff.php

回答by Morfildur

For PHP 5.3 you might want to use
http://de.php.net/manual/en/datetime.diff.php

对于 PHP 5.3,您可能需要使用
http://de.php.net/manual/en/datetime.diff.php

For lower versions you might want to use something like: (Simple solution, notfastest or best)

对于较低版本,您可能想要使用以下内容:(简单的解决方案,不是最快或最好的)

$days = 0;
$months = 0;
$month = date("n", $time1);
while ($time1 < $time2)
{
    ++$days;
    if (date("n", $time1) != $month)
    {
        $month = date("n", $time1);
        $days = 0;
        $months++;
    }
    $time1 = mktime(0, 0, 0, date("n", $time1), date("j", $time1) + 1, date("Y"));
}

//Disclaimer: untested

//免责声明:未经测试

回答by GeekTantra

Timestamp is actually the number of seconds since Unix Epoch i.e. 1 Jan 1970. So differences in timestamps is actually seconds. So to convert seconds into days you just need to divide it by number of seconds in a day i.e. $Timestamp/(24x60x60). Same for the month $Timestamp/(24x60x60x30)

时间戳实际上是自 Unix Epoch 即 1970 年 1 月 1 日以来的秒数。因此时间戳的差异实际上是秒。因此,要将秒数转换为天数,您只需将其除以一天中的秒数,即 $Timestamp/(24x60x60)。相同的月份 $Timestamp/(24x60x60x30)

回答by Thirler

I don't think this is available in php. So you will have to use integer division (or normal division) and modulo operator.

我不认为这在 php 中可用。所以你将不得不使用整数除法(或正常除法)和模运算符。

For instance (example uses hours instead of ms but it is the same trick repeated):

例如(示例使用小时而不是毫秒,但重复相同的技巧):

$totalhours = 27;
$days = round($totalhours / 24);
$hours = $totalhours % 24;

This will give $days = 1 and $hours = 3

这将给 $days = 1 和 $hours = 3

As stated in other replies. Finding months is harder because the amount of days per month isn't fixed.

如其他回复所述。查找月份更难,因为每月的天数不是固定的。

回答by ZombieSheep

The biggest problem you are going to face here is defining months. Are you going to use 4 weeks = 1 month, 30 days = 1 month, etc... I would be tempted to either leave the figure as a number of days, or at most, convert to weeks and days

您将在这里面临的最大问题是定义月份。您打算使用 4 周 = 1 个月、30 天 = 1 个月等......我很想将数字保留为天数,或者最多转换为周和天

回答by Benoit

You can calculate date difference in day, with a function like this

您可以使用这样的函数计算一天中的日期差异

function diff_date_day($day , $month , $year , $day2 , $month2 , $year2){
  $timestamp = mktime(0, 0, 0, $month, $day, $year, 0);
  $timestamp2 = mktime(0, 0, 0, $month2, $month2, $year2);
  $diff = floor(($timestamp - $timestamp2) / (3600 * 24)); 
  return $diff; 
}

The make an assumption an average month is 30 days and calculate the number of months. It can be enough for some needs (displaying blog comment age, etc.) and totally unadapted for others.

假设平均一个月是 30 天,然后计算月数。它可以满足某些需求(显示博客评论年龄等)而完全不适合其他需求。

回答by poke

You might want to take a look at DateTime.diff, as it is able to express differences you would expect from a human when comparing months (like 01.01.2010 to 01.03.2010 is a difference of (exactly) three months).

您可能想看看DateTime.diff,因为它能够表达您在比较月份时对人类的期望差异(例如 01.01.2010 到 01.03.2010 是(正好)三个月的差异)。

回答by Blackbeard

Found a solution using MySQL to calculate the months:

找到了一个使用 MySQL 计算月份的解决方案:

SELECT TIMESTAMPDIFF(MONTH,'{$start_time_str}','{$end_time_str}') AS m

SELECT TIMESTAMPDIFF(MONTH,'{$start_time_str}','{$end_time_str}') AS m

Thanks to all involved.

感谢所有参与的人。

回答by Luke Cousins

I know this is old now, but I came across it when looking for something to calculate the time difference in the format of "3 years & 6 months" from a given dob in timestamp format. I wrote the following which somebody might find useful.

我知道这现在已经过时了,但是我在寻找一些东西来计算时间戳格式的给定 dob 的“3 年和 6 个月”格式的时差时遇到了它。我写了以下内容,有人可能会觉得有用。

// Determine age
$time_start = 'YOUR DOB';
$time_end = strtotime('today');
$years = 0;
$months = 0;
while ($time_start < $time_end){
    $tmp_time = strtotime('+ 1 year', $time_start);
    if ($tmp_time < $time_end){
        $years++;
        $time_start = $tmp_time;
    } else {
        $tmp_time = strtotime('+ 1 month', $time_start);
        if ($tmp_time < $time_end){
            $months++;
            $time_start = $tmp_time;
        } else {
            $time_start = $time_end;
        }
    }
}
$age = $years.' years &amp; '.$months.' months';