Get date from input form within PHP - php

I'm trying to retrieve a date from a date input form and I just can't get it to work properly. The code I'm using now only returns an error and this date: 1970-01-01. That is not correct and I would like to know how I can approach this. This will be used later to query a database table. I want the date to be basically just 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('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
~~~ EDIT ~~~
Fixed Solution for anyone wondering how it's done:
HTML
<form name="Filter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
<input type="submit" name="submit" value="Login"/>
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;

Validate the INPUT.
$time = strtotime($_POST['dateFrom']);
if ($time) {
$new_date = date('Y-m-d', $time);
echo $new_date;
} else {
echo 'Invalid Date: ' . $_POST['dateFrom'];
// fix it.
}

<?php
if (isset($_POST['birthdate'])) {
$timestamp = strtotime($_POST['birthdate']);
$date=date('d',$timestamp);
$month=date('m',$timestamp);
$year=date('Y',$timestamp);
}
?>

Related

How to echo all dates between two dates?

How to echo all dates between two dates?
for example:
From the date of 2020/07/29 to the date of 2020/08/02
I want to print the results
2020/07/29
2020/07/30
2020/07/31
2020/08/01
2020/08/02
The following code prints only to 2020/07/31
if(isset($_POST['submit'])){
$from_date= $_POST['from_date'];
$to_date = $_POST['to_date'];
for($from_date = $from_date ; strtotime("+$from_date Day") <= $to_date; $from_date++){
echo $from_date . "<br>";
}
}
Result:
2020/07/29
2020/07/30
2020/07/31
HTML FORM
<form action="" method="post">
<input type="date" name="from_date">
<input type="date" name="to_date" >
<input name="submit" type="submit" value="submit">
</form>
Thanks

how to input date (using input Form date) to txt using PHP?

I want to ask how to input date with format YYYYMMDD to txt file using PHP?
here my code, but I just can input it using text not as date form :
<a> From : </a> // to show date after input
<?php // to show date after input
echo file_get_contents(from.txt" ); // to show date after input
?> // to show date after input
<?php
$file = "/var/www/html/from.txt";
if(isset($_POST))
{
$postedHTML = $_POST['html'];
file_put_contents($file, $postedHTML);
}
?>
<form action="" method="post">
<?php
$content = file_get_contents($file);
echo "<textarea name='html'>" . htmlspecialchars($content) . "</textarea>";
?>
<input type="submit" value="Click to enter date" />
</form>
you try this:
<form action="" method="post">
<?php
$date = htmlspecialchars($content)
$display_date= date("Y-m-d", strtotime($date));
echo "<input type='date' name='html' value ='" . $display_date . "'>";
?>
<input type="submit" value="Click to enter date" />
</form>
try like this:
<form action="" method="post">
<?php
$content = file_get_contents($file);
echo"<input type='date' name='date'>";
?>
<input type="submit" value="Click to enter date" />
</form>

Getting the date in mysql using input type 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.

Substracting birthday year with the current year

I've started learning PHP, and just can't figure out how to accomplish this.
The basic question is to how to calculate age from current year..
I have to do it with a submit button, and that's the problem for me. I tried this:
<form name="form2" action="16.1.php" method="POST">
Birthday year:
<input type="post" name="post">
<input type="submit" value= "Verstuur" >
</form>
16.1php:
<?php
echo date ('Y') - $_POST['post'];
?>
Try updating this..
<input type="input" name="post">
type = "post" to type = "input"
Try this
if($_POST['submit'])
{
$byear = $_POST['bdate'];
$today = new DateTime();
$birthdate = new DateTime($byear);
$interval = $today->diff($birthdate);
echo $interval->format('%y years');
}
<form name="form2" action="" method="POST">
Birthday year:
<input type="text" name="bdate">(yyy-mm-dd)
<input type="submit" name="submit" value= "Verstuur" >
</form>

Form Submitting Incorrect Information to MySQL Database

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

Categories