How to modify date not filled in form for the current day?

1

I tried to do with isset , type:

$var1 = isset($_POST["namedocampo"]) ? $_POST["namedocampo"] : date('d/m/Y');

And with if , type:

if ($_POST["namedocampo"] == "") {

$var1 = date('d/m/Y');

}

But whenever the field is not filled in, the date 31/12/1969 appears.

The date entry field looks like this:

<input type="text"  id="iddocampo" name="namedocampo" class="form-control" maxlength="10" placeholder="dd/mm/aaaa" onkeyup="formatar('##/##/####', this, event)"></label>

And the output field looks like this:

<li class="list-group-item">
<span class="badge"><?php echo date("d-m-Y",strtotime($var1)); ?></span>
Data:
</li>
    
asked by anonymous 22.06.2015 / 21:11

2 answers

1

First check that your form's method is like POST

Here is an example:

<form action="arquivo.php" method="post">
    Data: <input type="text" name="data"/>
</form>

file.php

<?php
    $data = empty($_POST["data"]) ? date("d/m/Y") : $_POST["data"];
    echo $data;

For the strtotime to work the date must be in one of the patterns described in link

  

For your output try the code below

date("d/m/Y",strtotime(str_replace('/', '-', '27/05/1990'))); 
    
22.06.2015 / 21:34
1

What happens is as follows: If strtotime ($ var1) returns false then date ("d-m-Y", strtotime ($ var1)); sets the default date 12/31/1969.

To resolve this check strtotime ($ var1).

$time = strtotime($var1);
$date = ($time === false) ? '0000-00-00 00:00:00' : date('Y-m-d H:i:s', $time);
echo $date;
    
22.06.2015 / 21:45