How to Add 30 Minutes to a Date in PHP

How to Add 30 Minutes to a Date in PHP thumbnail
Perform date arithmetic with PHP.

In PHP, a date consists of both a date and a time. If a time is not specified, PHP defaults to the current time. PHP stores a date as an integer representing the number of seconds since January 1 1970 00:00:00 GMT (Greenwich Mean Time). While PHP provides a number of built-in functions that allow you to create, convert and manipulate dates, you can add 30 minutes to a date by converting minutes to seconds and simply adding it to the variable, since a date is stored in PHP as seconds.

Instructions

    • 1

      Create a new variable and set it equal to the date to which you want to add 30 minutes by using the PHP "strtotime" function, since you will be using the time component of the date variable. For example, type:

      <?php

      $base = strtotime("next Tuesday");

    • 2

      Create a variable and set it equal to the number of minutes you want to add to the date. Convert the minutes into seconds by multiplying by 60. Add the seconds to the date you just created. For example, type:

      $minutes_to_add = 30;

      $seconds_to_add = $minutes_to_add * 60;

      $new = $base + $seconds_to_add;

    • 3

      Write some output to the screen that allows you to check the program's logic. Use the "date" function to display the date and time according to the desired format. For example, type:

      echo "Base date is " . date("j F Y g:i a", $base);

      echo "Minutes to add are $minutes_to_add";

      echo "New date is " . date("j F Y g:i a", $new);

Related Searches:

References

  • Photo Credit Jupiterimages/Comstock/Getty Images

Comments

You May Also Like

  • How to Add Minutes to DateTime

    C++ is a programming language featuring a vast library of functions that work with Microsoft Windows, and has many built-in value types....

  • How to Make a Date in PHP

    PHP is a programming language used to create dynamic webpages. Dynamic webpages allow individuals and businesses to supply users with content that...

  • How to Convert a Time Stamp to a Date in PHP

    The PHP programming language has several useful functions for getting and manipulating the date and time. The PHP "time()" function is used...

  • How to Echo Variables Using PHP

    The echo function (or more precisely a language construct) is one of the most frequently used functions in PHP. All programming languages...

  • How to Add Hours and Minutes in Excel

    When you enter numbers in Microsoft Excel, the program assumes that you are using the standard base-10 number system. Therefore, if you...

  • How to Add Minutes to TracFone

    There are three methods available to add minutes to a Tracfone. All of them can be completed with your Tracfone and pin...

Related Ads

Featured