Adding minutes to a timestamp

Discussion in 'PHP' started by assgar, Aug 8, 2007.

  1. #1
    :cool:Hi


    How can I add 15 minutes to the previous event time (timestamp) to get the next event time.
    Note: This event time is not in a table.

    Straight forward adding the two values will probly not provide the correct result.

    I can get the correct time with Mysql addtime($time, $duration) but that
    is only good if the if info is in a table.

    Thanks

    
    <?php
    $time = "09:00:00";//timestamp hh:mm:ss
    $duration = 00:15:00;//enent duration hh:mm:ss
    
    
    $next_time = $event_time + $event_duration;
    ?>
    
    PHP:
     
    assgar, Aug 8, 2007 IP
  2. mrburns

    mrburns Peon

    Messages:
    9
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i would use the unix timestamp which is in seconds, then add 900.

    $now=time();

    #there are 900 seconds in 15 minutes

    $fifteenminutes=$now + 900;

    now use the date function to format your time
     
    mrburns, Aug 8, 2007 IP
  3. veckd

    veckd Peon

    Messages:
    1,065
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that's what I would do as well
     
    veckd, Aug 8, 2007 IP
  4. mrburns

    mrburns Peon

    Messages:
    9
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or if you have a specific time, convert to a timestamp then add 900 seconds.
    $timestamp=mktime ( $hr,$min,$sec, etc. );
    $fifteenminutes= $timestamp +900;
     
    mrburns, Aug 8, 2007 IP
  5. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you wanna use timestamps (usually it's easier, but not always), use strtotime
     
    decepti0n, Aug 8, 2007 IP
  6. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    :cool:Hi

    You are great!
    Thanks for the suggestion.
    This is the final code that solved the problem

    
    <?
    	$event_time = 09:00:00
    	$event_length = 15;
    	
    	$timestamp = strtotime("$event_time");
    	$etime = strtotime("+$event_length minutes", $timestamp);
    	$next_time = date('H:i:s', $etime);
    ?>
    
    PHP:
     
    assgar, Aug 8, 2007 IP