使用格式“y-m-d”格式化自纪元以来的天数表示的日期,而不使用DateInterval

时间:2022-01-22 21:29:11

Look, I know we should move on and install the newest php already. But I can't. So I'm stuck with this piece of code I just got from a freelancer:

看,我知道我们应该继续安装最新的php。但我不能。所以我坚持使用我刚从*职业者那里得到的这段代码:

function daysToDate($days) {
    $interval = DateInterval::createFromDateString("+".round($days)." days");

    $d0 = new DateTime("1970-01-01");
    $d1 = $d0->add($interval);
    $res = $d1->format("Y-m-d");

    return $res;
}

This returns a string representation of the date denoted by the amount of days since the epoch ("1970-01-01"). My problem is that I'm getting the following error message:

这将返回由纪元(“1970-01-01”)以来的天数表示的日期的字符串表示形式。我的问题是我收到以下错误消息:

Fatal error: Class 'DateInterval' not found

致命错误:找不到类'DateInterval'

Looking this up on the internet, I found out that DateInterval is for PHP >= 5.3. I'm running 5.2. I already had to code a workaround for the inverse function when I was testing this on my PC:

在互联网上查找,我发现DateInterval适用于PHP> = 5.3。我正在运行5.2。当我在PC上测试时,我已经不得不为反函数编写一个变通方法:

/*
 * given a timestamp in the format 'Y-m-d h:i:s' (e.g. '2011-01-21 13:55:00'),
 * returns the count of days since the epoch ('1970-01-01 00:00:00')
 *
 * BUGFIX: I am using the strtotime here instead of DateInterval::days,
 * since that field is not set correctly in windows versions of PHP
 * (see PHP Bug #51184)
 */
function dateToDays($timestampstr) {
    $SECONDS_PER_DAY = 86400;

    $t = strtotime($timestampstr);

    return $t / $SECONDS_PER_DAY;
}

Now, on my Test-Server, I found I don't have access to DateInterval at all.

现在,在我的测试服务器上,我发现我根本无法访问DateInterval。

I'm going off for lunch now and will reward any successful at coding a PHP 5.2 version with a bouquet of internets, an accept and and upvote.

我现在要去吃午餐了,并且会奖励任何成功编写PHP 5.2版本的一系列互联网,一个接受和upvote。

1 个解决方案

#1


4  

Seems pretty easy to get that working in 5.2

似乎很容易在5.2中工作

<?php
function daysToDate52($days) {
    return date("Y-m-d", strtotime("+$days days", 0));
}

var_dump(daysToDate(20));
var_dump(daysToDate52(20));

string(10) "1970-01-21"
string(10) "1970-01-21"

Hope thats what you wanted. The use of DateIntervall is kinda pointless there anyways (imho at least :) )

希望这就是你想要的。无论如何使用DateIntervall有点无意义(imho至少:))

If you want use the DateTime (PHP 5.2) object and not strtotime then it would look something like this (only a small change to your code from the question):

如果你想使用DateTime(PHP 5.2)对象而不是strtotime那么它看起来像这样(只是对你的代码的一个小改动):

function daysToDateWithObject($days) {
    $x = new DateTime("1970-01-01");
    $x->modify("+$days days");
    return $x->format("Y-m-d");
}

#1


4  

Seems pretty easy to get that working in 5.2

似乎很容易在5.2中工作

<?php
function daysToDate52($days) {
    return date("Y-m-d", strtotime("+$days days", 0));
}

var_dump(daysToDate(20));
var_dump(daysToDate52(20));

string(10) "1970-01-21"
string(10) "1970-01-21"

Hope thats what you wanted. The use of DateIntervall is kinda pointless there anyways (imho at least :) )

希望这就是你想要的。无论如何使用DateIntervall有点无意义(imho至少:))

If you want use the DateTime (PHP 5.2) object and not strtotime then it would look something like this (only a small change to your code from the question):

如果你想使用DateTime(PHP 5.2)对象而不是strtotime那么它看起来像这样(只是对你的代码的一个小改动):

function daysToDateWithObject($days) {
    $x = new DateTime("1970-01-01");
    $x->modify("+$days days");
    return $x->format("Y-m-d");
}