PHP Calculating future date by adding days to a variable date

前端 未结 6 1772
一整个雨季
一整个雨季 2020-12-10 05:40

I was looking at this post, and it is close to what I need: PHP - How to count 60 days from the add date

However, in that post, the calculation is performed by addi

相关标签:
6条回答
  • 2020-12-10 06:22

    Use date_add

    http://www.php.net/manual/en/datetime.add.php

    $my_date = new DateTime($some_row_from_a_database);
    $date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
    
    0 讨论(0)
  • 2020-12-10 06:23

    You can put something before the "+10 days" part:

    strtotime("2010-01-01 +10 days");
    
    0 讨论(0)
  • 2020-12-10 06:37
    date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
    

    this will get the calculated date in defined format.

    0 讨论(0)
  • 2020-12-10 06:38

    You will have to look into strtotime(). I'd imagine your final code would look something like this:

    $dateVariable      = strtotime('2017-01-29');//your date variable goes here
    $date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
    echo $date_plus_60_days;
    

    If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:

    $date_plus_60_days = new DateTime("2006-12-12");
    $date_plus_60_days->modify("+60 days");
    echo $date_plus_60_days->format("Y-m-d");
    
    0 讨论(0)
  • 2020-12-10 06:39

    Suppose today's date is

    date_default_timezone_set('Asia/Calcutta');
    $today=date("Y-m-d");
    

    And i can add 10 days in current date as follows :

    $date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));
    
    0 讨论(0)
  • 2020-12-10 06:40

    I see you are retriving data from a database. If you are using mysql you can do it on the select:

    Example: you need the last date of the table and this date-7 days

    select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
    from table
    

    It´s easy use curdate() if you want todays date.

    If you need a dynamic between that selects the count of last 7 days:

    select count(*) from table
    where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
    
    0 讨论(0)
提交回复
热议问题