Yunfei Chen
Yunfei Chen

Reputation: 626

PHP how do you set the default date to a new one?

I tried to find how to set a date field to current date if the user does not fill it in but I am out of ideas can someone help me?? So for example if you user does not choose a date the date part should be today's date: 09/25/2020... Here's the basic html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width-device-width, intial-scale-1.0">
        <link rel="stylesheet" href="styles/main.css">
        <title>Find rest - add Infection</title>
    </head>
    <nav>
        <a href="index.html">Home</a>
    </nav>
        <body>
        <?php 
        //php invalid.
            if($_SERVER["REQUEST_METHOD"] == "POST"){
                $error_end = "";
                if(!$validEnd){
                    echo "Invalid end date: Please enter date after March 1 2020... \r\n";
                    $error_end = 'border: red 5px solid';
                }
            }
        ?>
<form>
        <table>
        <tr class="repeat_example">
                    <td>end_date <br><input type="date" name="end_date" id="end_date" value="<?php echoIfExists($_POST,"end_date","")?>" 
                    style="<?php echo $error_end; ?>"></td>
                    <td>date</td>
                    <td>The end_date of the infection if left empty
                        it should default to current date.</td>
                </tr>
        </table>
<br><br>
            
                <label> Write new Form!</label><input type="submit">
            </form>
        </body>
    </div>
</html>

And here's my PHP:

<?php

//constants
define("endDate", "end_date");

//Helper function
function valueIfExists($array, $key, $default){
    if(array_key_exists($key,$array)){
        return $array[$key];
    } else {
        return $default;
    }
}

if($_SERVER['REQUEST_METHOD'] == "POST"){

    $endTime = new dateTime(valueIfExists($_POST, endDate, "0"));
    $ref = new dateTime("03-01-2020");
    $current = new dateTime(strtotime(date("m-d-Y")));
    
    if($endTime->getTimeStamp() < $ref->getTimeStamp()){
        $isValid = false;
        $validEnd = false;
    }
    if($endTime->getTimeStamp() == "0"){
        $isDefault = true;
    }
}else {
    $isValid = false;
}



//Routing
if($isValid){    
    //process
    include "echo.php";
    exit(); //Emmit echo page if form is valid
} else {
    include "add_infection.php";
    exit(); //Emit the form if form is not valid
}

So if the user does not input in another date I want the form for the date part to display the current date...

thanks,

Upvotes: 0

Views: 382

Answers (1)

HTML5 date input defines the format as ISO8601.

You can set the default value of an input as

<input type="date" value="2020-09-25">

and this is obtained with the following PHP code:

date('Y-m-d');

so:

$date = isset($_POST['end_date']) ? $_POST['end_date'] : date('Y-m-d');

Upvotes: 1

Related Questions