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

Learn More

How to calculate dates for past and future days [PHP]

368 Runs 2596 Views 442 Copies
Saved

Saved

dipesh.parmar 55

dipesh.parmar
published 4 years ago

<div style="margin-left:50px;">
  <h2>Calculating dates for past and future days</h2>
  <!--
    we will use strtotime php function,
    which allow us to generate timestamp from the string we passed into this function,
    below code will demonstrate it how we can do this.
    and we will pass that into date php function's second argument,
  -->
  <h4>
    Shown below are the results of various strings passed to 
    <strong>strtotime</strong> and converted to date strings.
  </h4>
  <p>
    syntax for this would be 
    <strong>date('l jS F (Y-m-d)', strtotime('-3 days'))</strong>
    where <strong>l jS F (Y-m-d)</strong> represent the date format,
    and second argument is the strtotime function which will
    convert -3 days to 3 days before timestamp,
  </p>
  <h2>Examples</h2>
  <? echo "<strong>3 days ago :</strong> ".date('l jS F (Y-m-d)', strtotime('-3 days')); ?>
  <br/>
  <? echo "<strong>yesterday :</strong> ".date('l jS F (Y-m-d)', strtotime('yesterday')); ?>
  <br/>
  <? echo "<strong>+3 days :</strong> ".date('l jS F (Y-m-d)', strtotime('+3 days')); ?>
  <br/>
  <? echo "<strong>-2 weeks Saturday :</strong> ".date('l jS F (Y-m-d)', strtotime('-2 weeks Saturday')); ?>
  <br/>
  <? echo "<strong>next Saturday :</strong> ".date('l jS F (Y-m-d)', strtotime('next Saturday')); ?>
</div>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).