Shifting HOUR in time variable

Discussion in 'PHP' started by SNaRe, Jul 20, 2008.

  1. #1
    I have stored dates on my mysql.

    Example
    07-20-08 02:54:03 AM

    I want to shift date for 2 hours so it will automaticly be

    07-20-08 04:54:03 AM

    How can i convert that time to 2 hours later.
    I tried too many things
    like
    date("m-d-y h:i:s A", strtotime("+2 hours",$date));
    PHP:
    etc but i couldn't succeed it .
     
    SNaRe, Jul 20, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    date("m-d-y h:i:s A", strtotime($date) + (2*60*60));
     
    matthewrobertbell, Jul 20, 2008 IP
  3. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #3
    there is an addtime function for mysql

    havent tried this myself

    SELECT ADDTIME(yourdatefield, '02:00:00')
     
    serialCoder, Jul 20, 2008 IP
  4. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #4
    <?
    $date = "07-20-08 02:54:03 AM";
    echo date("m-d-y h:i:s A", strtotime($date) + (2*60*60));
    
    //output 01-01-70 04:00:00 AM
    ?>
    PHP:
    Now we are in year 2070 :D
     
    SNaRe, Jul 20, 2008 IP
  5. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #5
    have you tried the solution i posted? i havent tested it myself, would love to know if it indeed work
     
    serialCoder, Jul 20, 2008 IP
  6. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    date("m-d-y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")));
     
    rob_v, Jul 20, 2008 IP
    SNaRe likes this.
  7. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #7
    <?
    $date = "07-20-08 02:54:03 AM";
    echo date("m-d-y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")));
    ?>
    PHP:
    I have also a $date variable this must be on that $date

     
    SNaRe, Jul 20, 2008 IP
  8. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?php
    
    $oldDate = '02/20/2008';
    $newDate = new DateTime($oldDate);
    $newDate->modify('+2 Days');
    
    echo $newDate->format('m-d-Y h:i:s a');
    PHP:
     
    ahowell, Jul 21, 2008 IP
  9. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    here you go this is the easy way

    $date = "07-20-08 02:54:03 AM";
    echo date("m-d-y h:i:s A", strtotime ("+2 hour"));
    PHP:
     
    cornetofreak, Jul 21, 2008 IP
  10. ad network

    ad network Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ad network, Jul 21, 2008 IP