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

Learn More

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

377 Runs 2688 Views 457 Copies


dipesh.parmar 55

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,
    Shown below are the results of various strings passed to 
    <strong>strtotime</strong> and converted to date strings.
    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,
  <? echo "<strong>3 days ago :</strong> ".date('l jS F (Y-m-d)', strtotime('-3 days')); ?>
  <? echo "<strong>yesterday :</strong> ".date('l jS F (Y-m-d)', strtotime('yesterday')); ?>
  <? echo "<strong>+3 days :</strong> ".date('l jS F (Y-m-d)', strtotime('+3 days')); ?>
  <? echo "<strong>-2 weeks Saturday :</strong> ".date('l jS F (Y-m-d)', strtotime('-2 weeks Saturday')); ?>
  <? echo "<strong>next Saturday :</strong> ".date('l jS F (Y-m-d)', strtotime('next Saturday')); ?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).