Get date from input form in PHP

I am trying to get a date from a date input form and I just can't get it to work correctly. The code I'm using now returns an error and this date: 1970-01-01. This is not true, and I would like to know how I can do this. This will be used later to query the database table. I want the date to be basically like a string with this format: "yyyy-mm-dd"

code:

HTML

<form name="DateFilter" method="POST"> From: <input type="date" name="dateFrom" value="<?php echo date('Ym-d'); ?>" /> <br/> To: <input type="date" name="dateTo" value="<?php echo date('Ym-d'); ?>" /> </form> 

Php

 $new_date = date('Ym-d', strtotime($_POST['dateFrom'])); echo $new_date; 

Thanks in advance,

~ Realization

~~~ EDIT ~~~

Fixed solution for everyone who wondered how to do this:

HTML

 <form name="Filter" method="POST"> From: <input type="date" name="dateFrom" value="<?php echo date('Ym-d'); ?>" /> <br/> To: <input type="date" name="dateTo" value="<?php echo date('Ym-d'); ?>" /> <input type="submit" name="submit" value="Login"/> </form> 

Php

 $new_date = date('Ym-d', strtotime($_POST['dateFrom'])); echo $new_date; 

Special thanks for every reply.

+10
source share
2 answers

Confirm your entry.

 $time = strtotime($_POST['dateFrom']); if ($time) { $new_date = date('Ym-d', $time); echo $new_date; } else { echo 'Invalid Date: ' . $_POST['dateFrom']; // fix it. } 
+5
source
  <?php if (isset($_POST['birthdate'])) { $timestamp = strtotime($_POST['birthdate']); $date=date('d',$timestamp); $month=date('m',$timestamp); $year=date('Y',$timestamp); } ?> 
0
source

Source: https://habr.com/ru/post/987220/


All Articles