0

In my database date is saved as Y-M-D in the Date format. But I dont want to input in that way. Is it possible to convert D-M-Y before searching in database? In my code the input form get the date of today.

Code:

HTML

  <form name="input" action="" method="POST">
  Fra Dato: <input type="text" name="fraDato" value="<?php echo date('Y-m-d'); ?>" /> <br>
Til Dato: <input type="text" name="tilDato" value="<?php echo date('Y-m-d'); ?>"> <br> 
<input type="submit" name="submit" value="Visualiser" > <br>
  <input type="checkbox" id="PH" value="PH" >PH<br>
<input type="checkbox" id="Chlorine" value="Chlorine" >Chlorine<br>
<input type="checkbox" id="Temperature" value="Temperature" >Temperature<br> 

</form> 

PHP:

<?php
  $timezone = "Europe/Oslo";
  date_default_timezone_set($timezone);



if(isset($_POST['submit']))
{

$fraDato=$_REQUEST['fraDato'];
$tilDato=$_REQUEST['tilDato'];



$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("chart", $con);

$sth = mysql_query("SELECT PH, Chlorine, Temperature, Date, Time FROM googlechart where Date between '".$fraDato."' and '".$tilDato."' order by Date, Time");

?>

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user3270211
  • 915
  • 4
  • 20
  • 42
  • 1
    There's thousands of answers here explaining how to convert date strings from one format to another, e.g http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php – Mark Baker Mar 20 '14 at 10:41
  • check these links [enter link description here][1] [enter link description here][2] [1]: http://stackoverflow.com/questions/2487921/convert-date-format-yyyy-mm-dd-dd-mm-yyyy [2]: http://stackoverflow.com/questions/10306999/php-convert-date-format-dd-mm-yyyy-yyyy-mm-dd – Himanshu Mar 20 '14 at 10:46

1 Answers1

1

Yes you can concert dates easily with DateTime::createFromFormat,

$date = DateTime::createFromFormat('d-m-Y', $_REQUEST['fraDato']);
echo $fraDato = $date->format('Y-m-d');
Rikesh
  • 26,156
  • 14
  • 79
  • 87