How to find out what the date was 5 days ago?

后端 未结 10 1646
一整个雨季
一整个雨季 2020-12-24 04:46

Well, the following returns what date was 5 days ago:

$days_ago = date(\'Y-m-d\', mktime(0, 0, 0, date(\"m\") , date(\"d\") - 5, date(\"Y\")));
相关标签:
10条回答
  • 2020-12-24 05:06
    define('SECONDS_PER_DAY', 86400);
    $days_ago = date('Y-m-d', time() - 5 * SECONDS_PER_DAY);
    

    Other than that, you can use strtotime for any date:

    $days_ago = date('Y-m-d', strtotime('January 18, 2034') - 5 * SECONDS_PER_DAY);
    

    Or, as you used, mktime:

    $days_ago = date('Y-m-d', mktime(0, 0, 0, 12, 2, 2008) - 5 * SECONDS_PER_DAY);
    

    Well, you get it. The key is to remove enough seconds from the timestamp.

    0 讨论(0)
  • 2020-12-24 05:09

    Simply do this...hope it help

    $fifteendaysago = date_create('15 days ago');
    echo date_format($fifteendaysago, 'Y-m-d');
    
    0 讨论(0)
  • 2020-12-24 05:12

    Try this

    $date = date("Y-m-d", strtotime("-5 day"));
    
    0 讨论(0)
  • 2020-12-24 05:16

    If you want a method in which you know the algorithm, or the functions mentioned in the previous answer aren't available: convert the date to Julian Day number (which is a way of counting days from January 1st, 4713 B.C), then subtract five, then convert back to calendar date (year, month, day). Sources of the algorithms for the two conversions is section 9 of http://www.hermetic.ch/cal_stud/jdn.htm or http://en.wikipedia.org/wiki/Julian_day

    0 讨论(0)
  • 2020-12-24 05:19

    5 days ago from a particular date:

    $date = new DateTime('2008-12-02');
    $date->sub(new DateInterval('P5D'));
    echo $date->format('Y-m-d') . "\n";
    
    0 讨论(0)
  • 2020-12-24 05:20

    Use the built in date_sub and date_add functions to math with dates. (See http://php.net/manual/en/datetime.sub.php)

    Similar to Sazzad's answer, but in procedural style PHP,

    $date = date_create('2008-12-02');
    date_sub($date, date_interval_create_from_date_string('5 days'));
    echo date_format($date, 'Y-m-d'); //outputs 2008-11-27
    
    0 讨论(0)
提交回复
热议问题