Created a PHP input form to POST dates into SQL query, on the server side we have 0000-00-00 date format but locally we use the 00/00/0000 format, how do get the input form to accept the 00/00/0000 format to be used in the SQL query? By now we force the users to put the 0000-00-00 and it works perfectly but a lot of them are complaining about that!
<form action="index.php" method="POST">
<cust>From: </cust><input name="start_date" placeholder=" 0000-00-00" />
<cust>Until: </cust><input name="end_date" placeholder=" 0000-00-00" />
<select name="RegioSelect" placeholder="Choose"><option></option>
<?php
$region = $_POST["RegioSelect"];
$fdate = $_POST["start_date"];
$edate = $_POST["end_date"];
if(empty($fdate)){
$fdate = '2000-01-01';
} else {
$fdate;
}
if(empty($edate)){
$edate = date("Y/m/d");
} else {
$edate;
}
We were thinking convert on server side but in that case we can't use dates to compare date ranges. We convert them after retrieving to user's info as follows:
$new_fdate = strtotime($fdate);
$FiDate = date('d/m/Y',$new_fdate);
changed '$edate' to date("Y-m-d");
$new_edate = strtotime($edate);
$EnDate = date('d/m/Y',$new_edate);
?>
<input type="submit" value="Select region">
</select></br>
</form>
<?php
if(empty($region)){
$region = ' ';
} else {?><h4><?php echo $region; ?></h4><cust><b>From:</b><?php echo $FiDate; ?> <b>Until:</b> <?php echo $EnDate; ?></cust>
<?php
}
?>
Solved this as follows, now it works other side around, the input form accept 00/00/0000 date format but get converted to server side once POSTED.
<form action="list.php" method="POST">
<cust>From: </cust><input name="start_date" placeholder=" 00/00/0000" />
<cust>Until: </cust><input name="end_date" placeholder=" 00/00/0000" />
<select name="RegioSelect" placeholder="Choose"><option></option>
<?php
$region = $_POST["RegioSelect"];
//ORIGINAL VARIABLES
/*$fdate = $_POST["start_date"];
$edate = $_POST["end_date"];
*/
$fdate = date_format(date_create_from_format('d/m/Y', $_POST["start_date"]), 'Y-m-d');
$edate = date_format(date_create_from_format('d/m/Y', $_POST["end_date"]), 'Y-m-d');
if(empty($fdate)){
$fdate = '01/01/2000';
} else {
$fdate;
}
if(empty($edate)){
$edate = date("Y/m/d");
} else {
$edate;
}
So the code tells the query to choose all data between $fdate and $edate, if $fdate is empty take 01/01/2000 as first date and if $edate is empty take TODAY() as end date.
Related
Help please, am trying to validate a form date input not empty and should not be greater that today's date. this is what I did so far. am getting 000-00-00 inserted in MySQL db. what am I doing wrong?
here is what in the form input
<div class="form-group">
<label>Join Date</label>
<input type="date" name="joindate" class="form-control <?php echo (!empty($joindate_err)) ? 'is-invalid' : ''; ?> " value="<?php echo $joindate ?>">
<span class="invalid-feedback"><?php echo $joindate_err; ?></span>
</div>
the php tag above has this validations
//date validation
$input_joindate = trim($_POST["joindate"]);
if (empty($input_joindate)) {
$joindate_err = "Select join date";
}
if (!empty($input_joindate)) {
$input_joindate = date('Y-m-d', strtotime($input_joindate));
$today = strtotime("now");
if (($input_joindate) > $today)
$joindate_err = "Date should not be in the future";
} else {
$joindate = $input_joindate;
}
<?php
$today = date("Y-m-d");
$joindate = $_POST["joindate"];
if (empty($joindate) || !isset($joindate)) {
$joindate_err = "Select join date";
} elseif (strtotime($today) < strtotime($joindate)) {
$joindate_err = "Date should not be in the future";
}
I want to insert data into table when selected date is Current Date or Yesterday's Date.
html code:
<input type="text" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="<?php echo date('d/m/Y'); ?>">
php code:
if(isset($_POST['submit'])){
$report_date=date('Y-m-d',strtotime($_POST['reportdate']));
$check_date=date('d',strtotime($_POST['reportdate']));
if($check_date!=date('d') or $check_date!=date('d')-1){
echo "<h2>You can only submit dcr of current date or yesterday.</h2>";
}
else{
$client=$_POST['CLIENT_NAME'];
//$cars_string = implode(', ', $_POST['cars']);
$productname_string=implode(', ',$_POST['productname']);
$remarks=$_POST['remark'];
$sql="insert into d_c_r (bm,date_time,client,promoted_product,remark) values ('".$_SESSION['id']."','$report_date',$client,'$productname_string','$remarks')";
$res=mysql_query($sql) or die(mysql_error());
}
}
Working Code Updated:
Working Demo: http://phpfiddle.org/main/code/fxh8-gnkw
<html>
<body>
<form action="" method="post" name="myform">
<input type="date" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="">
<button type="submit" name="submit"> Submit</button>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && !empty($_POST['reportdate'])){
$today_date = date('d-m-Y'); // you get current date
$date_tday = new DateTime($today_date);
$date_tday->modify('-1 day');
$get_yesterday_date_from_current = $date_tday->format('d-m-Y'); // you get Yesterday date that from current date
$reportdate = $_POST['reportdate']; // you get report date that user is selected
$report_date_format = new DateTime($reportdate);
echo $report_date = $report_date_format->format('d-m-Y'); //you get report date that user is selected in d-m-Y
/*
$date = new DateTime($report_date);
$date->modify('-1 day');
echo $get_yesterday_date = $date->format('d-m-Y'); // you get Yesterday date that user is selected
*/
if($report_date !== $today_date && $report_date !== $get_yesterday_date_from_current){
echo "<h2>You can only submit dcr of current Date or Yesterday.</h2>";
}
else{
echo "Inser Your Data OR What You WAnt To Do HERE";
}
}
?>
I don't have a background in PHP but a recent client requires some updating so i'm doing my best. What I'm trying to accomplish is first update the form to accept the correct date format to covert using the code I created that will through an error if it's less than 24 hours from the present time. The form now:
<p>
<label for="date">Date:<span>*</span></label>
<span>
<input type="text" name="date" id="date" value="<?php $this->value('date'); ?>" size="25" />
</span>
</p>
<p>
<label for="time">Eat Time:<span>*</span></label>
<span>
<input type="text" name="time" id="time" value="<?php $this->value('time'); ?>" size="10" />
</span>
</p>
<p>
<label for="timedes">AM or PM:<span>*</span></label>
<span>
<select name="timedes" id="timedes">
<option value="0" <?php $this->value('timedes', '0', 'selected="selected"'); ?>>Select..</option>
<option value="AM" <?php $this->value('timedes', 'AM', 'selected="selected"'); ?>>AM</option>
<option value="PM" <?php $this->value('timedes', 'PM', 'selected="selected"'); ?>>PM</option>
</select>
</span>
</p>
The code that I will need to modify to validate if it's less than 24 hours or not.
// define variables for date
date_default_timezone_set('America/New_York'); // timezone
$dateInput = '10/11/2015 15:54'; //date they selected string
$cateringDate = (strtotime("$dateInput")); // date selected converted to time
$dateNow = date('m/d/y H:i'); // current date and time string
$currenttime = (strtotime("$dateNow")); // current date and time converted
$dateAllowed = $currenttime + (86400); // current date and time 24 hours added on
$difference = $dateAllowed - $cateringDate; // date selected vs current date + 24hours
if ($cateringDate >= $dateAllowed) {
echo 'good job';
} else {
echo 'try again';
}
There are multiple ways to do this. But here is one using strtotime:
date_default_timezone_set('America/New_York'); // timezone
$dateInput = '10/11/2015 15:54';
if (strtotime('+24 hours') < strtotime($dateInput)) {
echo 'good job';
} else {
echo 'try again';
}
If strtotime did not understand the format it returns false, so:
if (false === strtotime($dateInput)) {
echo 'Sorry, what?';
}
Use the datetime class and take advantage of the diff method, i.e.:
date_default_timezone_set( 'America/New_York' ); // timezone
$date1 = new DateTime( 'NOW' );
$dateInput = '10/10/2015 15:54';
$date2 = DateTime::createFromFormat( 'm/d/Y H:i', $dateInput );
$diff = $date2->diff( $date1 );
if( $diff->format( '%d' ) != 0 ){
echo "good job";
}else{
echo "try again";
}
We check if the difference (in days) between NOW and the $dateInput is different from 0, zero means less than 24h from a future or previous date.
I want to get the date in mysql using input type="date" .. please help for the code...
here is my code:
<form action="sales.php" method="get">
<input type="date" name="d1" value="" required="required" />
<input id="btn" type="submit" value="Search">
</form>
Here is my connection:
<?php
if (isset($_GET[ "d1"])) {
$do=$_GET[ "d1"];
} else {
$do=0;
};
$result=$ db->prepare("SELECT * FROM sales WHERE date=:a");
$result->bindParam(':a', $do);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
Thanks in advance
Inside that sales.php script print out:
$date_value = $_GET['d1'];
echo $date_value;
$myslq_date = date ('Y-m-d H:i:s', strtotime ($date_value));
But check for browser compatibility for that input type - not sure how it's supported.
I've created a form that submits data to a MySQL database but the Date, Time, Year and Month fields constantly revert to the exact same date (1st January 1970) despite the fact that when I submit the information to the database the form displays the current date, time etc to me. I've already set it so that the time and date fields automatically display the current time and date. Could someone please help me with this.
Form:
<html>
<head>
<title>Ultan's Blog | New Post</title>
<link rel="stylesheet" href="css/newposts.css" type="text/css" />
</head>
<body>
<div class="new-form">
<div class="header">
<img src="images/edit-home-button.png">
</div>
<div class="form-bg">
<?php
if (isset($_POST['submit'])) {
$month = htmlspecialchars(strip_tags($_POST['month']));
$date = htmlspecialchars(strip_tags($_POST['date']));
$year = htmlspecialchars(strip_tags($_POST['year']));
$time = htmlspecialchars(strip_tags($_POST['time']));
$title = htmlspecialchars(strip_tags($_POST['title']));
$entry = $_POST['entry'];
$timestamp = strtotime($month . " " . $date . " " . $year . " " . $time);
$entry = nl2br($entry);
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$entry = addslashes($entry);
}
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('tmlblog');
$sql = "INSERT INTO php_blog (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')";
$result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error());
if ($result != false) {
print "<p class=\"success\">Your entry has successfully been entered into the blog. </p>";
}
mysql_close();
}
?>
<?php
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />
<input type="text" name="time" id="time" size="5"value="<?php echo $current_time; ?>" />
<input class="field2" type="text" id="title" value="Title Goes Here." name="title" size="40" />
<textarea class="textarea" cols="80" rows="20" name="entry" id="entry" class="field2"></textarea>
<input class="field" type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</div>
<div class="bottom"></div>
<!-- //End Wrapper!-->
</body>
</html>
</html>
For some reason the posts are being submitted without a timestamp and are reverting to a default timestamp.
Sounds like your form is giving your database a very low amount of "Ticks". Notice you have an with name="date" and id="date" three times. This is most likely your problem. Change those names and ids, do use month, year, date (according to your "postback").
You have three text boxes named date. Since $_POST['month'] and $_POST['year'] are not set, strtotime() cannot figure out the correct timestamp and the SQL query looks like this:
INSERT INTO php_blog (timestamp,title,entry) VALUES ('','Title Goes Here.','')
Change the names of the input elements to what your PHP code is expecting:
<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />
After you've done this, strtotime() will be able to parse the date and insert the correct value into the query.
INSERT INTO php_blog (timestamp,title,entry) VALUES ('1272738360','Title Goes Here.','')
It could be that strtotime fails to recognize the format you are submitting your date in.
Try a classic YYYY/MM/DD format:
$timestamp = strtotime("$year/$month/$date $time");
Looks like strtotime don't understand the format you feeding to it.
Use mktime instead.
Anyway you have to always debug your code, i.e. print out the query to see if anything goes wrong.
Or consider to use mysql-supported type colymn, date or datetime
You may be confused an unix timestamp which is just number ans mysql timestamp type column which is string of 2010-05-01 00:00:00