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:
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:
Fatal error: Class ‘DateInterval’ not found
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:
/*
* 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.
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.
Seems pretty easy to get that working in 5.2
Hope thats what you wanted. The use of DateIntervall is kinda pointless there anyways (imho at least 🙂 )
If you want use the
DateTime(PHP 5.2) object and notstrtotimethen it would look something like this (only a small change to your code from the question):