• We just launched and are currently in beta. Join us as we build and grow the community.

Subtract Days to the Current Date using PHP

Dark03

SaaS Builder
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
83
Likes
116
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will teach you on how to subtract days in a date using PHP.
I uses PHP strtotime to subtract days to the current date. Follow the steps bellow to learn this tutorial.

Creating Our Form

Tis code will display our form with the current date and a textbox that allow you to input the number of days you want to subtract in your current date.
Copy the code bellow and save it as "index.php".

  1. <form

    action

    =

    "index.php"

    method

    =

    "GET"

    >
  2. <span

    style

    =

    "display: inline-block; width: 150px;"

    >

    Date</

    span

    ><input

    type

    =

    "text"

    name

    =

    "date"

    required=

    "required"

    value

    =

    "<?php echo date("

    m/

    d/

    Y") ?>

    " /><br

    >
  3. <span

    style

    =

    "display: inline-block; width: 150px;"

    >

    Number of days</

    span

    ><input

    type

    =

    "text"

    name

    =

    "numberofdays"

    value

    =

    "<?php echo $nd ?>

    " autocomplete="off" /><br

    >
  4. <span

    style

    =

    "display: inline-block; width: 150px;"

    >

    Result</

    span

    ><input

    type

    =

    "text"

    name

    =

    "result"

    value

    =

    "<?php echo $result ?>

    " /><br

    >
  5. <span

    style

    =

    "display: inline-block; width: 150px;"

    >

    &nbsp;

    </

    span

    ><input

    type

    =

    "submit"

    value

    =

    "Calculate"

    /

    >
  6. </

    form

    >

Creating Our Script That add Days to Date

Copy the code bellow and paste it above our form in "Index.php".

  1. <?php
  2. if

    (

    isset

    (

    $_GET

    [

    "date"

    ]

    )

    )

    {

    $date

    =

    $_GET

    [

    "date"

    ]

    ;

    }

    else

    {

    $date

    =

    date

    (

    "m/d/Y"

    )

    ;

    }

    ;
  3. if

    (

    isset

    (

    $_GET

    [

    "numberofdays"

    ]

    )

    )

    {

    $nd

    =

    $_GET

    [

    "numberofdays"

    ]

    ;

    }

    else

    {

    $nd

    =

    0

    ;

    }

    ;
  4. $result

    =

    date

    (

    'm/d/Y'

    ,

    strtotime

    (

    $date

    .

    ' - '

    .

    $nd

    .

    ' days'

    )

    )

    ;
  5. ?>

Summary of Our index.php

The code bellow provides you the exact arrangement of our script
  1. <?php
  2. if

    (

    isset

    (

    $_GET

    [

    "date"

    ]

    )

    )

    {

    $date

    =

    $_GET

    [

    "date"

    ]

    ;

    }

    else

    {

    $date

    =

    date

    (

    "m/d/Y"

    )

    ;

    }

    ;
  3. if

    (

    isset

    (

    $_GET

    [

    "numberofdays"

    ]

    )

    )

    {

    $nd

    =

    $_GET

    [

    "numberofdays"

    ]

    ;

    }

    else

    {

    $nd

    =

    0

    ;

    }

    ;
  4. $result

    =

    date

    (

    'm/d/Y'

    ,

    strtotime

    (

    $date

    .

    ' - '

    .

    $nd

    .

    ' days'

    )

    )

    ;
  5. ?>
  6. <form action="index.php" method="GET">
  7. <span style="display: inline-block; width: 150px;">Date</span><input type="text" name="date" required="required" value="<?php

    echo

    date

    (

    "m/d/Y"

    )

    ?>

    " /><br>
  8. <span style="display: inline-block; width: 150px;">Number of days</span><input type="text" name="numberofdays" value="<?php

    echo

    $nd

    ?>

    " autocomplete="off" /><br>
  9. <span style="display: inline-block; width: 150px;">Result</span><input type="text" name="result" value="<?php

    echo

    $result

    ?>

    " /><br>
  10. <span style="display: inline-block; width: 150px;">&nbsp;</span><input type="submit" value="Calculate" />
  11. </form>

That's it you've been successfully created a script that subtract days to the current date using strtotime function in PHP.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top