Some days ago I wrote an article on how to get the local time. But I saw that many people are searching with keywords like “PHP Add Time”. As this is happening frequently, I have decided to write an article on how to add time using PHP. Here is the article. In this article, I have written a function that takes hour, minute, second, day, month and year as inputs, adds all supplied time values and returns the new time.

So, you can add hours, minutes, seconds, days, months and years with current time by supplying only 6 inputs when you call the function.

Okay. Here is the PHP function to add time.

<?php

function addTime($hours=0, $minutes=0, $seconds=0, $months=0, $days=0, $years=0)

{

$totalHours = date(“H”) + $hours;

$totalMinutes = date(“i”) + $minutes;

$totalSeconds = date(“s”) + $seconds;

$totalMonths = date(“m”) + $months;

$totalDays = date(“d”) + $days;

$totalYears = date(“Y”) + $years;

$timeStamp = mktime($totalHours, $totalMinutes, $totalSeconds, $totalMonths, $totalDays, $totalYears);

$myTime = date(“Y-m-d H:i:s A”, $timeStamp);

return $myTime;

}

?>

Now we call the function to test the output.

<?php

// Let us first see the current time

echo ‘Current Time: ‘ . date(“Y-m-d H:i:s A”);

echo ‘<BR>’;

// Now let us add 2 hours and 10 minutes from now

echo ‘New Time: ‘ . addTime(2,10,0,0,0,0);

?>

Thus, this is so simple to add time using PHP.

So, I hope the people who were looking for a PHP example to add time and eventually came to my article on how to find local time would get the right article.

You can also download the PDF version of this article from the link below:
PDF version of PHP script to add time. Tutorial with code and example

5 Responses to “PHP script to add time. Tutorial with code and example”

  1. Tim Lavelle Says:

    This works pretty good, except for one issue. It can’t distinguish between AM and PM. If I addtime to change the day by 1, and that new time happens to be 2:00am, the time shows as 2:00pm regardless of the time.

  2. Tim Lavelle Says:

    Nevermind, needed to put -hours to read it properly. Script works like a charm! Thanks for it!

  3. tanzilo Says:

    Hello Tim Lavelle,

    Many thanks for your comment.

    OK.
    I added 1 day to change the day by 1 this way:
    ——————————————
    <?php

    // Let us first see the current time

    echo ‘Current Time: ‘ . date(“Y-m-d H:i:s A”);

    echo ”;

    // Now let us add 2 hours and 10 minutes from now

    echo ‘New Time: ‘ . addTime(0,0,0,0,1,0);

    ?>
    ——————————————

    And this is the output:
    Current Time: 2008-03-07 00:15:37 AM
    New Time: 2008-03-08 00:15:37 AM

    Tim,
    Did you try like this?
    If not, then could you please tell me the problem in details?
    I want to debug the problem in the script (if any).

    As a writer of the script,
    I have the full responsibility to debug the code.

    Thanks for your comment once again.

  4. dreamluverz Says:

    Wow.Thanks for sharing your code. We’ll be trying this out.

  5. Elvis Says:

    If you change
    $myTime = date(“m/d/Y h:i A T”, $timeStamp);

    echo “Current Time: ” . date(“m/d/Y h:i A T”);
    echo addTime(2,0,0,0,0,0);

    I get the correct numbers, but the add time for some reason outputs AM even if its PM when the hours are in “h” 12 hour setting.


Leave a Reply