Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:50:41+00:00 2026-05-23T06:50:41+00:00

Look, I know we should move on and install the newest php already. But

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:50:41+00:00Added an answer on May 23, 2026 at 6:50 am

    Seems pretty easy to get that working in 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 🙂 )

    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):

    function daysToDateWithObject($days) {
        $x = new DateTime("1970-01-01");
        $x->modify("+$days days");
        return $x->format("Y-m-d");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can look at the row.count or tables.count, but are there other
Anybody know what my DLLImport statement should look like here: extern C __declspec(dllexport) long
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
I don't know where to look for about this problem ... I have three
I know that Flash Catalyst is still in Beta, but I'm trying to look
I guess this query is a little basic and I should know more about
I know there's an easy way of doing this, but my recursion abilities are
I don't know what is going on here but I have the following code
I know that I can use .Min() to get minimum value from column, but
This has been asked before , I know, but the answers are a bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.