How To Add Seconds, Minutes, Hours, Days, Months And Years To A Date In PHP | CampCodes

How to Add Seconds, Minutes, Hours, Days, Months and Years to a Date in PHP

June 17, 2020
add date and time in php

In this tutorial, I’m going to show you how to add several seconds, minutes, hours, days, months or years using PHP. This tutorial will not give you a good design but will provide you with an idea on the topic. Also, If you wanted to convert our output date, you may do so by visiting my previous tutorial about Converting Date Formats.

Creating Form and Output Date

We determine our output date by user input upon submission. We name this form as “index.php”.

<!DOCTYPE html>
<title>How to Add Seconds, Minutes, Hours, Days, Months and Years to a Date and Time in PHP</title>
    <h2>Our Date and Time: 2017-08-28 13:05:05</h2>
        $olddate="2017-08-28 13:05:05";
    <form method="POST">
        <input type="text" name="number">
        <select name="what">
            <option value="seconds">Seconds</option>
            <option value="minutes">Minutes</option>
            <option value="hours">Hours</option>
            <option value="days">Days</option>
            <option value="months">Months</option>
            <option value="years">Years</option>
        <input type="submit" name="add" value="Add">
        if (isset($_POST['add'])){
            echo "<br> You have added ".$number." ".$what.".";
            $newdate=date('Y-m-d H:i:s', strtotime("+$number $what", strtotime($olddate)));
    <h2>New Date and Time: <?php echo $newdate; ?> </h2>

Happy Coding!

Download Code
READ ALSO:   How to Insert Data into MySQL Database using AngularJS in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *