<!doctype html>
<?php
  date_default_timezone_set("America/New_York");
?>
<html>
  <head>
    <title>Working with the Date Object and DateInterval() Function</title>
    <link href="https://jimgerland.com/includes/css/styles.css" rel="stylesheet" />
    <link href="https://jimgerland.com/includes/css/codestyles.css" rel="stylesheet" />
  </head>
  <body>
    <h1>How to use the <code>Date</code> Object and the <code>DateInterval()</code>Function Together</h1>
    <h2>How to add a DateInterval object to a DateTime object</h2>
    <div style="font-family: Courier, Arial, Sans-Serif; font-size: large;">
    <code>
     $checkout_length = new DateInterval('P3W');<br /> 
     $due_date = new DateTime(); <br />
     $due_date->add($checkout_length);
    </code>
    </div>
    <h2>How to subtract a DateInterval object from a DateTime object</h2>
    <div style="font-family: Courier, Arial, Sans-Serif; font-size: large;">
    <code>
    $voting_age = new DateInterval('P18Y');<br />
    $dob = new DateTime(); $dob->sub($voting_age);<br />
    echo("&lt;p&gt;You can vote if you were born on or before ' . $dob->format('n/j/Y') . "&lt;/p&gt;\n");
    </code>
    <?php
    $voting_age = new DateInterval('P18Y');
    $dob = new DateTime(); $dob->sub($voting_age);
    echo("<p>You can vote if you were born on or before " . $dob->format('n/j/Y') . "?p>\n");
    ?>
    <h2>How to determine the amount of time between two dates</h2>
    <code>
    $now = new DateTime('2020-09-13 12:45:00');<br />
    $due = new DateTime('2021-04 last day of midnight');<br />
    $time_span = $now->diff($due);<br />
    echo("&lt;p&gt;"Time between " . $now->format('m/d/Y h:m') . " and " . $due->format('m/d/Y h:m') . " is: " . $time_span->format('%R%dd %H:%I:%Sh') . "&lt;/p&gt;\n"); // '-15d 12:45:00h'
    </code>
    <?php
    $now = new DateTime('2020-09-13 12:45:00');
    $due = new DateTime('2021-04 last day of midnight');
    $time_span = $now->diff($due);
    echo("<p>Time between " . $now->format('m/d/Y h:m') . " and " . $due->format('m/d/Y h:m') . " is: " . $time_span->format('%R%dd %H:%I:%Sh') . "</p>\n"); // '-15d 12:45:00h'
    ?>
    <ul>
      <li>You can use a DateInterval object to represent a time span called a date interval.</li>
      <li>To display a date part with a leading zero, change its format code to uppercase.</li>
    </ul>
    </div>
    <p><a href=".">Back</a></p>
  </body>
</html> 