Skip to content

Set a date in the future with PHP

PHP Code Snippets > Set a date in the future with PHP

by Steve Dawson Web Developer


If you have some sort of shopping cart or delivery system on your website and need to set a date in the future for delivery selection, this handy little PHP snippet will show you the way.

Simple copy and paste the code below into your website and it will display a drop down list which is set for any days in the future. It does not include weekends.

You can choose to set a date in the future which is handy as you may be going on holiday or your shop is closed. The example below is set for 7 days from today and will run for 21 days. You can change both these variables to suit your requirements.


The example...


 7 days in the future running for a period of 21 days

The PHP Code to use is...


<?php 
        
echo "<select name=\"proposed_deliver_date\">";        
        
$from_date 7;
        
$duration  21;        
        
$end_stamp strtotime("+" . ($from_date $duration) . "days");
        for (
$i strtotime("+ $from_date days"); $i <= $end_stamp$i += 86400) {    
        if (
date("N",$i) < 21) { 
        
// remove the weekends ...
        
if (date("l",$i) == "Saturday" || date("l",$i) == "Sunday"){ echo ""; } 
        else {    echo 
"<option value=\"".date("l jS F Y",$i)."\">".date("l jS F Y",$i)."</option>
        "
; }
        } }
        echo 
"</select>";
?>

Be aware you may need to set the date_default_timezone_set() value for your PHP to prevent any warning errors in your log files.

Free Quote available for any custom PHP and Database Software, Contact Steve Dawson Today