Reputation: 31
I have some PHP code:
$time = new DateTime();
$sub = new DateInterval('P1H');
echo $time->sub($sub);
I get this error:
Fatal error: Uncaught exception 'Exception' with message
'DateInterval::__construct(): Unknown or bad format (P1H)'
in C:\xampp\htdocs\dailylog\test.php:4 Stack trace: #0
C:\xampp\htdocs\dailylog\test.php(4): DateInterval->__construct('P1H')
#1 {main} thrown in C:\xampp\htdocs\dailylog\test.php on line 4
What am I doing wrong? How could I get this to work?
EDIT: I guess I'll actually be pulling the time entered from the database and checking if has been + 1 hour. So, sorry for not being clear.
EDIT #2: Since I cant answer my own question...here's what I was going to say:
I did a test on my own and it works perfect! Here's the code:
try {
$time = new DateTime();
$add = new DateInterval('PT1H');
$test = new DateTime();
$ing = new DateInterval('PT1H');
$time->add($add);
$test->add($ing);
if ($time->format('g:i a') == $test->format('g:i a')) {
echo 'Match';
}
else {
echo 'No match';
}
}
catch (Exception $e) {
echo $e->getMessage();
}
Upvotes: 1
Views: 1090
Reputation: 219924
You have a format issue with your DateInterval
value and a syntax error as well:
<?php
$time = new DateTime();
$sub = new DateInterval('PT1H'); // <-- error #1
$time->add($sub); // <-- error #2
echo $time->format('c'); // Here you can format your time
Upvotes: 2
Reputation: 324790
Try just running strtotime
on the value stored in MySQL, then add 3600
to it and compare it to time()
(you want to allow access if the modified value is less than time()
)
Upvotes: 0