How to get previous year using PHP

前端 未结 9 750
生来不讨喜
生来不讨喜 2021-02-01 01:30

How am I able to get the value of the previous year using PHP. Are there any predefined functions for it?

相关标签:
9条回答
  • 2021-02-01 01:43

    There are many ways, you can either take away the amount of seconds in a year from time() like so:

    $prevYear = date('Y', time() - 60*60*24*365 );

    Or if you'd prefer, use the PHPs clever strtotime() function:

    $prevYear = date('Y', strtotime('-1 year'));

    Or even like others have said, if it's from todays year just do date('Y') -1

    0 讨论(0)
  • 2021-02-01 01:44
    function affffdate($vardate,$added)
    {
    $data = explode("-", $vardate);
    $date = new DateTime();            
    $date->setDate($data[0], $data[1], $data[2]);
    $date->modify("".$added."");
    $day= $date->format("Y-m-d");
    return $day;    
    }
    
    echo "Example : " . affffdate("2010-08-01","-1 year");
    
    0 讨论(0)
  • 2021-02-01 01:49

    Shortest approach:

    $lastYear = (int)date("Y") - 1;
    
    0 讨论(0)
提交回复
热议问题