Speed up development with full-stack environments for every branch.

Learn More

How to adds and substracts some days, months, years, hours, minutes, and seconds to a date [PHP]

308 Runs 2377 Views 894 Copies
Saved

Saved

dipesh.parmar 55

dipesh.parmar
published 4 years ago

<div style="margin-left:50px;">
  <h2>Add Day to Specific Date</h2>
  <?
    /*
      date_create : return new DateTime object.
    */
    $add_date = date_create('2013-10-25');
    
    /*
      date_add : adds some days,
      months, years, hours, minutes, and seconds to a date.
      
      date_interval_create_from_date_string :
        Sets up a DateInterval from the relative parts of the string,
        you can use `2 weeks` OR `3 months` OR `1 day + 12 hours`
        OR `3600 seconds` etc instead of
        `10 days` in current example.
    */
    date_add($add_date, date_interval_create_from_date_string('10 days'));
    
    /*
      date_format : used to display date/time data in different format
    */
    echo date_format($add_date, 'Y-m-d');
  ?>
  <hr/>
  <h2>Substract Day from Specific Date</h2>
  <?
    $sub_date = date_create('2013-10-25');
    /*
      date_sub : substract some days,
      months, years, hours, minutes, and seconds to a date.
    */
    date_sub($sub_date, date_interval_create_from_date_string('10 days'));
    
    /*
      date_format : used to display date/time data in different format
    */
    echo date_format($sub_date, 'Y-m-d');
  ?>
</div>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).