|
-
Jan 21st, 2012, 11:41 AM
#1
Thread Starter
Hyperactive Member
check if its one hour before time
So I am trying to check if the current time is one hour before a variable time:
Code:
$date_game=$dt->format('Y n j'.$pieces[2]);
echo $date_game;
echo date('Y n j H');
if (date('Y n j H') < $date_game) {
echo "The time is before the stored time";
}
This displays
2012 1 21 17:30
2012 1 21 16
i.e $pieces[2] = 17:30. and $dt formatted Y n j = 2012 1 21.
The current Y n j H is 2012 1 21 16.
I want to know if it is more than one hour until the date/time stored in $date_game.
At the moment it just tells me that it is before that time.
Can I do something like
Code:
$data_game - 1->format('H');
or something?
Thanks guys
-
Jan 22nd, 2012, 02:26 AM
#2
Re: check if its one hour before time
You can't compare the times after they've been formatted as strings. The point of formatting it is to make it readable by humans.
The good news is it looks like you're already using the DateTime class, which means you can do this:
PHP Code:
# if $dt is DateTime of the game $now = new DateTime(); $diff = $now->diff($dt); if ($dt > $now && $diff->h >= 1) { # game is >= 1 hour in the future }
-
Jan 23rd, 2012, 06:15 AM
#3
Thread Starter
Hyperactive Member
Re: check if its one hour before time
Ok my worry is it wouldnt have any regard for the day either. I.e if the game is on the 27th at 4.00pm would it know that today at 5pm is still before that game?
-
Jan 23rd, 2012, 06:04 PM
#4
Re: check if its one hour before time
Good point; I forgot about that. This ought to do it:
PHP Code:
$dt = new DateTime('24 January 2012 16:00'); $now = new DateTime();
$deadline = $dt->sub(new DateInterval('PT1H')); if ($now < $deadline) { # game is >= 1 hour in the future }
In this snippet, $deadline is a DateTime 1 hour before the game ($dt). 'PT1H' means an duration specification of 1 hour ('P' for period; 'T' for time; 'H' for hour).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|