Reputation: 15
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.
Upvotes: 0
Views: 3748
Reputation: 649
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');
Upvotes: 1
Reputation: 825
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
Upvotes: 0