This morning I was wondering what the date would be an arbitrary number of days in the future, like if somebody says: get back to me in a few months. I decided to search Google to see if there was a simple, web-based form that I could enter in the number of days in the future and my timezone and get returned the date/time in the future.
A couple variations of queries didn’t turn up anything that fit what I was looking for, so I decided to build my own and share the process here. For this project we’ll be using the following built-in PHP functions:
count - get the size of the timezonelist array
time - get timestamp
strtotime - add form submitted amount of time
date - format the date
putenv - change timezone
Data needed
List of timezones inserted into a sorted array. I’ve done the work here and provide in an array called $timezonelist. This will be used in the dropdown input form so the person using can set to their timezone.
Security concerns
To prevent users from entering in bogus timezones we’ll tie to a valid array index and check before using putenv() to change.
STEP 1. Add a comment header to explain the code and licensing terms:
<?php
/* Created: 12/21/2006
daysinfuture2.php -> original code by TDavid @ tdscripts.com
License: Creative Commons 2.5 Attribution
Desscription: Enter in days in the future and find out the date
*/
STEP 2. Add the sorted time zone list array and determine the array size using the count function. Due to the size of the array, I’m including only a couple entries in the code below. You’ll be able to view the entire array at the end of this post.
$timezonelist =
array(‘Africa/Abidjan’,
‘Africa/Accra’);
// incomplete timezone list, example only
$sizetz =
count($timezonelist);
STEP 3. Handle the input from the form submit, if any and validate the input as integers. The maximum number of days allowed into the future is 10,000.
$fdays =
(int
)$_POST[‘d’];
// number of days entered from form
$ftz =
(int
)$_POST[‘tz’];
// timezone chosen from
// 10,000 days max
if($fdays <0 || $fdays >10000) {
$fdays = 7; // week from now is default
}
// default timezone is Los Angeles / PST
if($ftz > $sizetz || $ftz < 0) {
$ftz = 107; // default timezone PST
}
STEP 4. Set the timezone using the index in the timezonelist array chosen by the person submitting the form or the default if an invalid or no selection was made.
putenv(“TZ=$timezonelist[$ftz]”);
STEP 5. Get the current time and calculate the number of days into the future.
$today =
time();
$future_tense =
strtotime(“+$fdays days”,
$today);
STEP 6. Output to the browser the current formatted date/time and future date/time. Then close the PHP script.
print “Now: “ .
date(“l M d, Y H:i:s”,
$today) .
” <i>$timezonelist[$ftz]<br /><font color=\”green\”><b>+ $fdays days =</b></font> <b>” .
date(“l M d, Y H:i:s”,
$future_tense) .
‘</b>’;
?>
STEP 7. Create the first part of the form for the person to submit to the script the days into the future and timezone. At the top of the list select the USA Los Angeles time (PST) as default. The values for the options are the index position in the timezone array.
<form method=“POST” action=“daysinfuture2.php”>
Number of days in the future?
<input type=“text” size=“3″ name=“d” value=“<?php echo($fdays);?/>"> Timezone? (pending)
<select name=”tz“>
<option SELECTED value=”107“>USA - Pacific (GMT -8)</option>
<option value=”70“>USA - Midwest (GMT -6)</option>
<option value=”122“>USA - Eastern (GMT -5)</option>
STEP 8. Dynamically generate all sorted timezones from the array.
<?php
for($y=
0;
$y<
$sizetz;
$y++
) {
print(“<option value=\”$y\”>$timezonelist[$y]\n“);
}
?>
STEP 9. Now close the HTML form code and we’re done.
</select> <input type=“submit” value=“See future date”/>
</form>
Test execution of daysinfuture2.php
Complete source code