How to Add 30 Minutes to a Date in 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);
-
1
References
- Photo Credit Jupiterimages/Comstock/Getty Images