Results 1 to 4 of 4

Thread: check if its one hour before time

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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->>= 1)
    {
        
    # game is >= 1 hour in the future


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    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?

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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
  •  



Click Here to Expand Forum to Full Width