Date/Time Question

Discussion in 'PHP' started by nate45, Feb 6, 2007.

  1. #1
    I have a form that I would like a user to enter "X" amount of minutes. The form will post to another php page for output to the user. What I would like to do is use the value of "X" minutes - (minus) the current server time to determin what the date and time would have been "X" minutes ago.

    In other words...If I said that something occured 10 minutes ago on the form, then I want PHP to figure out what the Date & Time was 10 minutes ago, and echo it to the user.

    I have searched everywhere, but I cannot find the exact code that I would need to achieve this...any help would be much appreciated!
     
    nate45, Feb 6, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    echo date("d M Y - H:i:s", time() - ($x * 60));
     
    phper, Feb 6, 2007 IP
  3. nate45

    nate45 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That worked perfectly...thanks for your time!!!

    There is just one more thing that I think I need regarding time.

    I am also wanting to echo the amount of "x" minutes to the user. But if x is greater than 1 hour, I would like for PHP to echo x hour(s), and x minutes(s).
    Instead of displaying 144 minutes, I would like it to display 2 hours, 24 minutes.


    Thanks again...
     
    nate45, Feb 6, 2007 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    echo "Duration: " . floor($x / 60) . " hours " . ($x % 60) . " minutes";

    I'm sure you'll be able to work out the if statement yourself :)
     
    phper, Feb 6, 2007 IP
  5. nate45

    nate45 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Works great..

    This is my if statement:

    if ( $down_minutes < 60 ) {
    echo $down_minutes . " minutes<br /><br />";
    } elseif ( $down_minutes == 60 ) {
    echo "" . floor($down_minutes / 60) . " hour <br /><br />";
    } else {
    echo "" . floor($down_minutes / 60) . " hour(s) " . ($down_minutes % 60) . " minute(s)<br /><br />";
    }

    I am sure that there has to be a better way for me to display "x hour" for every 60 minutes (see 3rd line)...but the only way I would know would be to do a bunch of if elseif statements for 60 minute increments??

    Thanks again!!!
     
    nate45, Feb 6, 2007 IP
  6. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Maybe this? Checks if the minutes / 60 is a whole number, if it is then it's obviously an "o'clock". :)

    Seems to work when I tested...

    if ( $down_minutes < 60 ) 
    {
    	echo $down_minutes . " minutes<br /><br />";
    } 
    elseif ( is_int($down_minutes / 60) )
    {
    	echo floor($down_minutes / 60) . " hour(s) <br /><br />";
    } 
    else 
    {
    	echo floor($down_minutes / 60) . " hour(s) " . ($down_minutes % 60) . " minute(s)<br /><br />";
    }
    PHP:
     
    chopsticks, Feb 7, 2007 IP
  7. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Pressed reply instead of edit...
     
    chopsticks, Feb 7, 2007 IP