0

I’m having a problem using PHP’s ‘diff()’ or ‘date_diff()’ between two dates of mine. Basically when I run anything that attempts to compare dates or try to define something as a date the page will stop loading at that point.

<?php

    // My starting point is the $dirty_date variable.
    // It is collected from a string, parsed, 
    // and ends up being an integer-based date / seconds past Unix Epoch

    //I am also including some echos along the way for debugging purposes.

    echo "Check 1: ".$dirty_date."<br>";

    $systemdate = date("U");
    echo "Check 2: ".$systemdate."<br>";

    //$interval =  $dirty_date->diff($systemdate);
    echo "check 3: ".$interval;

Here are the results:

Check 1: 1490781836

Check 2: 1490806703

check 3:

Check 3 returned nothing because I have the line which diffs it commented out. I have it commented out because if it is enabled, the page doesn't load anything else past that point.

8
  • 3
    -> is used for objects. error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Mar 29, 2017 at 18:45
  • 3
    looks like your $dirty_date is just an integer. You need \DateTime object. $dirty_date = \DateTime::setTimestamp($dirty_date) Commented Mar 29, 2017 at 18:46
  • php.net/manual/en/datetime.diff.php is a good start Commented Mar 29, 2017 at 18:47
  • @AbraCadaver Thanks. I'm very rusty in PHP / re-learning. So if my time/date variables are not object, then how do I compare them. I've also tried, what I assume is creating an object, by doing $variable = new DateTime($dirty_date); but that also halts the page loading at that point as well. Commented Mar 29, 2017 at 18:48
  • @SergeiKutanov Thanks. I tried $dirty_date = \DateTime::setTimestamp($dirty_date); but as soon as I hit that line the PHP stops loading the page. I confirmed it's that line by commenting it out. As soon as I do $dirty_date starts echo'ing again. Commented Mar 29, 2017 at 18:54

2 Answers 2

1

First, $newdate is not defined. Second, $dirty_date must be a DateTime object currently it is just an integer. I will assume you want to compare $dirty_date and $systemdate

Try this:

$dirty_date_obj = new DateTime();
$dirty_date_obj->setTimestamp((int) $dirty_date);

$system_date_obj = new DateTime();
$system_date_obj->setTimestamp((int) $systemdate);


echo "interval between dirty and system: ".$dirty_date_obj->diff($system_date_obj)->format('%a Days and %h hours');

Also please type this in the beginning of your code to see the error messages so it isn't just a blank page if some error happens:

error_reporting(E_ALL); ini_set('display_errors', '1');
Sign up to request clarification or add additional context in comments.

7 Comments

@Murila Thank you. That is a much different way of doing it than I've been attempting. I needed to study what you did there. This does run and it returns: interval between dirty and system: 17254 Days and 10 hours. The problem is, I was expecting maybe a few hours difference between those two datetime. Is something weird happening when converting from integers to dates?
@Mike I edited the answer. I added a new line on the $system_date_obj, please try it. I tested it with the integers you provided on Check1 and Check2 and got 0 days and 6 hours. Is that a result you would expect?
Yes, 6 hours seems about right. I was starting to employment this. I made some changes to variable names, and since my system time is screwy (for a certain reason) I needed to adjust the system time by -4 hours. Here is my code doing that. It runs. The starting and ending times are coorect, but when it does the different it's saying 45 minutes when really it should be about 360 minutes.
here is where I'm at now: ` $status_date = new \DateTime(); $status_date->setTimestamp($dirty_date); echo "Check 1: ".$status_date->format(\DateTime::ISO8601)."<br>"; // Get the current system date $systemdate = new \DateTime(); $systemdate->setTimestamp(strtotime("-4 hours",strtotime(date('Y-m-d H:i:s')))); echo "Check 2: ".$systemdate->format(\DateTime::ISO8601)."<br>"; $interval = $status_date->diff($systemdate); echo "check 3: ".$interval->format("%i minutes"); `
here is the output: Check 1: 2017-03-29T10:03:56+0000 Check 2: 2017-03-29T15:50:52+0000 check 3: 46 minutes
|
0

Here's an example

    $dirty_date = new \DateTime();
    $dirty_date->setTimestamp("your integer date");
    echo "Check 1: ".$dirty_date->format(\DateTime::ISO8601)."<br>";


    $systemdate = new \DateTime();
    echo "Check 2: ".$systemdate->format(\DateTime::ISO8601)."<br>";

    $interval =  $dirty_date->diff($systemdate);
    echo "check 3: ".$interval->format("%R%a days");

link to DateInterval ($interval var) formats http://php.net/manual/en/dateinterval.format.php

2 Comments

With some modification, this works! My integer date is $dirty_date. So I swapped $dirty_date with "your integer date" and renamed all of your $dirty_date variables to something else. I haven't worked with dates in format, so I need to study what you did here on more depth. My next step is that I have to adjust on of the dates by 4 hours, but I beleive I should be able to figure that out. Thank you very much for your help!
If my answer helped you, please accept it. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.