简体   繁体   English

将unix时间转换为DateTime对象:PHP 5.2.X

[英]Convert unix time to DateTime Object : PHP 5.2.X

Basically I want to get remaining time to expire. 基本上我想让剩余的时间到期。 I did it easily in php 5.3.x like below 我在php 5.3.x中轻松完成,如下所示

$now = new DateTime();
$future_date =  new DateTime($enddate);
$interval = $future_date->diff($now,false); // ->diff will not work on 5.2.x
$interval->format("%d d, %h h, %i m");

How can I do the something in PHP : 5.2.x(5.2.17) . 我怎样才能在PHP中做到这一点:5.2.x(5.2.17)

What I tried : 我尝试了什么:

new DateTime(date('Y-m-d H:i:s', $now->format('U') - $future_date->format('U')));

Not providing expected result. 没有提供预期的结果。

Because PHP < 5.3 doesn't support the DateInterval class, DateTime::diff() (which is the right way to do this) is unavailable. 因为PHP <5.3不支持DateInterval类,所以DateTime::diff() (这是执行此操作的正确方法)不可用。 You will need to do this manually for it to work in 5.2.x. 您需要手动执行此操作才能在5.2.x中运行。

The math is actually quite simple: 数学实际上非常简单:

// Get the difference between the two dates, in seconds
$diff = $future_date->format('U') - $now->format('U');

// Calculate the days component
$d = floor($diff / 86400);
$diff %= 86400;

// Calculate the hours component
$h = floor($diff / 3600);
$diff %= 3600;

// Calculate the minutes component
$m = floor($diff / 60);

// Calculate the seconds component
$s = $diff % 60;

// $d, $h, $m and $s now contain the values you want, so you can just build a
// string from them
$str = "$d d, $h h, $m m, $s s";

However with larger intervals this will introduce inaccuracies, because it does not take leap seconds into account. 但是如果间隔较大,则会引入不准确性,因为它不会考虑闰秒。 This means that you could end up a few seconds out - but since your original format string does not contain a seconds component, I doubt this will matter too much for what you are doing. 这意味着你可能会在几秒钟内结束 - 但由于你的原始格式字符串不包含秒组件,我怀疑这对你正在做的事情来说太重要了。

Note also that you need to subtract $now from $future_date , not the other way around, or the result will be negative. 另请注意,您需要从$future_date减去$now ,而不是相反,否则结果将为负数。

See it working 看它工作

What I have tried seems to work and is not so far away from what have You tried: 我试过的似乎工作,并没有你尝试过的东西:

echo $now = date('Y-m-d H:i:s');
echo PHP_EOL;
echo $future = date('Y-m-d H:i:s', strtotime('today + 3 days'));
echo PHP_EOL;
echo date('d', (strtotime("{$future}") - strtotime("{$now}")));

Check http://codepad.org/JbaJ3EGH 检查http://codepad.org/JbaJ3EGH

You could transform date() into DateTime easily then. 您可以轻松地将date()转换为DateTime It is also neccessary to get only the days, hours, minutes or months from the subtracted dates as formatting the "3 days interval" into a date would lead to a "1970-01-01 00:00:00.000" date... 由于将“3天间隔”格式化为日期将导致“1970-01-01 00:00:00.000”日期,因此从减去的日期仅获取日,小时,分钟或月份也是必要的...

It may be useful to know that, in DaveRandom's very helpful answer, the values returned from floor() are floats (doubles), so 知道在DaveRandom非常有用的答案中,从floor()返回的值是浮点数(双精度数)可能很有用,所以

$h === 1 ? true : false;

will return false even if var_dump($h) prints 1 . 即使var_dump($h)打印1也会返回false。

If you need to check for values being integers (say you need to check for values being 1 or greater than one when creating strings with singular and plural versions of time units), cast them as integers when assigning them: 如果需要检查整数值(比如在创建具有单数和复数版本的时间单位的字符串时需要检查值是1还是大于1),请在分配它们时将它们转换为整数:

$h = (int) floor($diff / 3600);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM