Midhun Raj
Midhun Raj

Reputation: 987

How to pass date input to php from form.It gives output as 1970-01-01

Here is an input form which passes date type to intermediate.php from a form.

<form action="intermediate.php" method="post">
<input type="date" name="vazhipadudate1" />
</form>

How can i get the picked date i tried this code snippet .It echos as 1970-01-01

Php code snippet.

$date='vazhipadudate1';
$time = strtotime($_POST["$date"]);
$storecart[$i]['date'] = date('Y-m-d', $time);
echo "Selected Date is..........".$storecart[$i]['date'] ;

The output i am getting as

Selected Date is..........1970-01-01

Upvotes: 1

Views: 866

Answers (1)

ezio4df
ezio4df

Reputation: 4125

After reproducing the problem,

I think the problem is probably because your submitting the form without passing any value to input. As The input has no def value. So it, POSTs empty string.

And when you pass the empty string to strtotime that returns false
Amd, again when you pass it to date that returns 1970-01-01 as (int) FALSE -> 0

So you should test the POST before processing it. Something like this to check POSTed data,

!empty( $_POST["vazhipadudate1"] )  
# AND/OR,
$time = strtotime($_POST["$date"]);
if (time === false) /*TakeAppropriateAction*/;

Upvotes: 2

Related Questions