Date minus 1 year ?

Date minus 1 year ?

Asked on December 24, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Use this code with strtotime for get solution,

    $date = strtotime('2010-01-01 -1 year');
    
    

    The output of strtotime function is a unix timestamp, to get the formatted string.

    So,we can use date  function in the code,

    echo date('Y-m-d', $date); // echoes '2009-01-01'
    
    
    Answered on December 24, 2018.
    Add Comment

    Try to use strtotime() function like the example below:

    $time = strtotime("-1 year", time());
    $date = date("Y-m-d", $time);
    
    Answered on December 24, 2018.
    Add Comment

    Here, we use the DateTime object.

    $time = new DateTime('2099-01-01');
    $newtime = $time->modify('-1 year')->format('Y-m-d');
    

    Similarly, use the now for today,

    $time = new DateTime('now');
    $newtime = $time->modify('-1 year')->format('Y-m-d');
    
    Answered on December 24, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.