1

While editing the php file it shows DateTime is not a valid method, plus when I try to use this it stops loading the rest of the page, do I have to implement something?

My goal is to check either if the difference between those variables is more than 3 minutes.

Can I output the difference just in minutes?

$arr["etime"] comes from a mysql query in the format of Y-m-d H:i:s.

$etime = $arr["etime"];
$datenow = date('Y-m-d H:i:s');

$datetimenow = new DateTime($datenow);
$datetimee = new DateTime($etime);
$datedifference = $datetimee->diff($datetimenow);
echo $datedifference->format("%H:%I:%S");

If I just put $date = new DateTime('2000-01-01'); it doesn't load the rest of the page after this.

If I put $datetimenow = new DateTime(); the rest of the page won't load.

Using exception handling, I received:

PHP Fatal error: Uncaught exception 'Exception'

with message:

'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.'

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Stephanie Sotelo
  • 75
  • 1
  • 1
  • 6

1 Answers1

1

You will only need to check the output object values generated from diff(): (Demo)

$etime = '2017-07-10 16:59:04';
$timezone=new DateTimeZone("Australia/Melbourne"); // declare whatever your timezone is
$datetimee=new DateTime($etime,$timezone);  // resultset datetime
$datetimenow=new DateTime("now",$timezone); // current datetime
$diff=$datetimee->diff($datetimenow);
//var_export($diff);
if($diff->i>=3 ||$diff->h>0 || $diff->d>0 || $diff->m>0 || $diff->y>0){
    echo "Difference is 3 minutes or more";
}else{
    echo "Difference is not yet 3 minutes";
}

If you are still experiencing issues with the DateTime class, it may be time to revert to good-ol' strtotime(). It provides a much simpler bit of code anyhow:

if(strtotime('now')-strtotime($etime)>179){ // assuming now is always bigger than etime
    echo "Difference is 3 minutes or more";
}else{
    echo "Difference is not yet 3 minutes";
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • $datetimenow = new DateTime($datenow); makes the rest of the page not load after this line, $datenow = date('Y-m-d H:i:s'); echo "$datenow _ $escrowtime\n"; works and outputs 2017-07-10 23:48:46 _ 2017-07-10 16:30:04 but if I put the other lines the rest of the page won't load. – Stephanie Sotelo Jul 10 '17 at 23:49
  • I can't, my work is under nda agreement, all I can say is that the line $datetimenow = new DateTime($datenow); does not work, while echo "$datenow _ $etime\n"; outputs 2017-07-10 23:48:46 _ 2017-07-10 16:30:04. I commented the rest of the code after $datetimenow = new DateTime($datenow); and it still won't load the page, while commenting it and leaving the first 2 lines the page loads everything and the output is fine. – Stephanie Sotelo Jul 10 '17 at 23:54
  • I did, still the rest of the page won't load, php version 5.4.16 (cli) – Stephanie Sotelo Jul 11 '17 at 00:09
  • @StephanieSotelo Please have a read of this: https://stackoverflow.com/questions/16019126/php-datetime-exception-and-errors-handling and do some testing to see if you can offer any new information for us. I am not able to replicate the error with the information you have provided (sample input and php version). – mickmackusa Jul 11 '17 at 01:28
  • PHP Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.' My face is like wtf right now.. – Stephanie Sotelo Jul 11 '17 at 01:42
  • I have updated my answer to include a timezone declaration. – mickmackusa Jul 11 '17 at 01:50
  • Edit, it still trows an error, I wasn't watching :\ Object of class DateTime could not be converted to string – Stephanie Sotelo Jul 11 '17 at 05:11
  • fixed, but $datetimee returns the same value as $datetimenow – Stephanie Sotelo Jul 11 '17 at 05:42