How To Subtract Time

Discussion in 'PHP' started by deriklogov, Mar 10, 2009.

  1. #1
    I am getting time with function
    date('Y-m-d h:i:s');
    but I need to subtract couple hours and minutes from current time, how I can do that ?

    Thank You
     
    deriklogov, Mar 10, 2009 IP
  2. shubhamjain1

    shubhamjain1 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It cannot be done directly but there is another way.!!

    Convert your date to unix timestamp using mktime(). It will only give a numerical value and substract it from current timestamp and it will give you the second difference.
     
    shubhamjain1, Mar 10, 2009 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    
    $currtime = date('Y-m-d h:i:s');
    
    $five_days_ago = strtotime($currtime." - 5 days"); // this is in timestamp format
    $five_days_ago_date = date("Y-m-d h:i:s", $five_days_ago); // converted back to your date format
    
    // another example
    $one_month_later = strtotime($currtime." + 1 month");
    $one_month_later_date = date("Y-m-d h:i:s", $one_month_later);
    
    
    PHP:
    In short,

    
    $five_days_ago_timestamp = strtotime(date('Y-m-d h:i:s')." - 5 days");
    
    PHP:
     
    ads2help, Mar 10, 2009 IP
  4. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #4
    on the last line of code, it's date('Y...) without the $ sign
     
    nabil_kadimi, Mar 10, 2009 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Thank you. Edited :eek: :D

    - ads2help
     
    ads2help, Mar 10, 2009 IP
    nabil_kadimi likes this.
  6. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,080
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #6
    Works Amazing , Thank You Very Much
     
    deriklogov, Mar 10, 2009 IP