how to set date field to 3 months back - php

I am using a radio button for fetching the record of last 3 months from database. I am using a radio button for the user to select the date range, for three months back.
How do I set the value of the radio button, to eaxctly 3 months back?
<input type="radio" id="test7" name="thismonth" value="<?php echo date("Y-m", strtotime("-3 months"));?>" />
This sets the value for only one month but is there way I set the value to exactly three months back?

<?php
$value = $_POST["thismonth"];
// note : name attribute for your input field is 'thismonth', should be something else.
$query = "SELECT *columns* FROM *table* WHERE *date_column* BETWEEN '".$value."' AND '".date('Y-m')."' ";
?>

try like this:
<?$effectiveDate=date('y-m-d');
$effectiveDate = strtotime("-3 months", strtotime($effectiveDate)); ?>
<input type="radio" id="test7" name="thismonth" value="<?php echo effectiveDate?>" />

Related

Posting a form with mysqli

I've created a web page working with php and mysqli in which I can send and edit MySQL entries without errors. The entries have title and date. The problem, appears when I try to change a date that is NULL.
Create an entry with date: Works
Create an entry without date: Works
Edit an entry with a date: Works
Edit an entry with a date to NULL: Works
Edit an entry with date NULL to an existing date: (doesn't work)
I've tried and tested things and I can assume that the problem is that the "day", "month" and "year" inputs are in blank/empty when the form is generated by PHP.
Also can't understand why the form to create an entry, which is exactly the same but with all the inputs in blank at the beginning, works in every case.
This is how I generate my form:
$mysqli->set_charset('Latin1');
$id=$_POST["id"];
$res = $mysqli->query("SELECT * FROM `list` WHERE `id`=$id");
while($row = $res->fetch_assoc()) {
if ($row["fecha"]==NULL) {$day="";$month="";$year="";}else{list($year, $month, $day) = explode("-", $row["date"]);};
echo "
<div class='home'>
<form accept-charset='Latin1' method='post' action='http://mypage.com/?opt=list&f=view'>
<input type='text' value='".$row['title']."' name='title' id='title' required><br>
<input type='number' value=".$day." name='day' id='day' min='1' max='31' placeholder='day'>
<input type='number' value=".$month." name='month' id='month' min='1' max='12' placeholder='month'>
<input type='number' value=".$year." name='year' id='year' placeholder='year'>
<input style='display:none;' type='number' value=".$id." name='id' id='id' placeholder='id'>
<input type='button' id='datepicker'><br>
<input type='submit' name='edit' value='Update'>
</form>
</div>";
};
And this is what happens when I submit:
$edit=$_POST["edit"];
if ($edit) {
$id=$_POST["id"];
$title=$_POST["title"];
if (empty($_POST["year"])&&empty($_POST["month"])&&empty($_POST["day"])){
$res = $mysqli->query("UPDATE `list` SET `title`=('$title'),`date`=(NULL) WHERE `id` = $id;");
}else{
$date=$_POST["year"]."-".$_POST["month"]."-".$_POST["day"];
$res = $mysqli->query("UPDATE `list` SET `title`=('$title'),`date`=('$date') WHERE `id` = $id;");
};
print ("Entry updated to ".$date); //When it doesn't works nothing appears in $date
};
I don't have any kind of idea of why it doesn't work JUST when the "day", "month" and "year" inputs are empty at the beginning.
PS. This is a solution (when date is NULL give values and then clear them when the form is generated), but I still don't know what is the problem with the inputs:
...
if ($row["fecha"]==NULL) {$day="0";$month="0";$year="0";}else{list($year, $month, $day) = explode("-", $row["date"]);};
echo "...";
if ($row["fecha"]==NULL){echo "<script>document.getElementById('day').value='';document.getElementById('month').value='';document.getElementById('year').value='';</script>";
...
Thanks to any help.
When you submit form, empty values from inputs are not submitted as null. They are submitted as empty string "" or zero in case of input which type is number.
Best way to avoid this is to use empty() function:
if (empty($_POST["year"]) && empty($_POST["month"]) && empty($_POST["day"])) {
// code ...
}
This function returns true when the variable is null, zero, false or empty.
Updated answer:
If you can't edit null date column to some date, make sure that:
That the column type of DATE
That your variables that contain year, month and day are formatted with precending zeros
Wrong examples: day = 5, month = 9
Good examples: day = '05', month = '09'
When you concate them as integers you get '2016-9-5' which is invalid date format.
So you should format them as strings with precending zeros like this '2016-09-05'.
Change this line of code:
$date=$_POST["year"]."-".$_POST["month"]."-".$_POST["day"];
To:
$date=sprintf("%04d-%02d-%02d", $_POST["year"], $_POST["month"], $_POST["day"]);
(Posted on behalf of the OP).
The problem was just syntax errors:
value= ' ".$day." '

Select date() HTML5 and match with SQL date

I am sooo stuck on this, cannot figure it out. I am trying to have the user select the date using the date selector in HTML5 and then query the sql database to find the date and output the results in a table below. I have been working on this for literally hours, cannot get past it, please help.
HTML
Date for retrieve: <input type="date" name="pickupDate" value="<?php echo $pickupDate;?>">
<span class="error">
</p>
<p>
Show date item for retrieve:
<input type="radio" name="input" value="requestDate1">Request Date
<input type="radio" name="input" value="pickupDate1">Pickup Date
</p>
<p><input type="submit" value="Show"></p>
$requestDate = mysqli_real_escape_string($db, isset($_POST["pickupDate"]));
PHP
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["input"] == "requestDate1") {
$query = "SELECT requestDate FROM request WHERE requestDate = '$requestDate'";
echo "working";
I think your problem is with this selection query:
$query = "SELECT requestDate FROM request WHERE requestDate = '$requestDate'";
Please change it in this way:
$query = "SELECT requestDate FROM request WHERE requestDate = '" . $requestDate . "';";
Or if it's not working then check the date format both on frontend and backend, and if it's needed change the date format on backend to match it with frontend.
This is simple:
SQL uses the YMD HIS format to store date in to it's field, and HTML5 uses DDMMYYYY so if you want to use this date format, your required to change the format, I suggest you go through this answer in stackoverflow for more details...
HTML 5 Date format

how to display the dob dd mm yyyy values from database to let the users edit it

I don't know if I asked the question properly or not..
Well I am having a form where I have a dob field and dd mm yyyy are taken from users with 3 textfields..
and I took those values and inserted into the db like dis..
$dob=$_POST('yyyy')+"-"+$_POST('mm')+"-"+$_POST('dd');
(Insert bla bla bla)
DOB <input type="text" name="dd">
<input type="text" name="mm">
<input type="text" name="yyyy">
Now I am having another form where I'm letting the users edit their info including the dob and update those..
Now I want to show their current dob in the editinfo page where 3 text fields will be same and i want to display the user's dob date in dd, dob month in mm,and dob year in yyyy field coming from database.
Just like we return the value in a text field like
<input type="text' value="<?php echo $name;?>
I want to do the same..but can't return the value of $dob because it will surely return in yyyy-mm-dd.I want the each value to be displayed in separate fields as I mentioned..
Any help will be deeply appreciated.. Thank you..!!!
You can use explode function to get each values. As you know the returning result from mysql will be in 'yyyy-mm-dd' format .
for example '2015-04-01' is the value . then
<?php
$date = '2015-04-01';
$splited_date_array = explode('-',$date);
$year = $splited_date_array[0];
$month = $splited_date_array[1];
$day = $splited_date_array[2];
?>

How to insert into database with input type=month?

billing.php
Expiry Date: <input type="month" id="ccexpirydate" name="ccexpirydate" value="<?php echo $row['expirydate'] ?>"/>
Firstly, how do I retrieve from database only showing month and year?
dobilling.php
$expiryDate = $_POST['ccexpirydate'];
$query = "INSERT INTO bill (ccexpirydate) VALUES ('" . $expiryDate . "')";
Secondly, after editing the expiry date and submitting the form, how to do I update the database with input type=month? Showing only month and year. As currently, my database would show 0000-00-00.
Date inputs are passed in the standard format. Regardless of what is shown to the user (which will be their local format), your server will receive YYYY-MM.
You can either save this as a CHAR(7) column, or add "-01" to the end of it and keep your current DATE column.
You can concatenate any random date to the variable while inserting into table, like
$expiryDate = $_POST['ccexpirydate'];
$expiryDate = $expiryDate."28"; //any date
And while retrieving and displaying in form, you can use it like:
<input type="month" value="<?php echo date('Y-m', strtotime($table_name['date_field'])); ?>"> //Y-m will give you date like, eg. 2017-11. Input type month will automatically read it as.. November 2017

Printing php page with various values

I have a php page that gets values from my database called print.php which I use to display and print the values from my database.
print.php?id=100 loads the page with values from the database that has the id 100. each record on my database has a field to store the date the record was created. As for now I visit print.php?id=100, print.php?id=101, print.php?id=102, print.php?id=103 and so on to print the same page but with various values.
Is there a way for me to create a button which does this automatically?
This button should grab all records that have the current date and print all those pages.
Sorry for being unclear.
My print.php page echos values from my database like this:
Id: <?php echo $info['id']; ?>
Name: <?php echo $info['name']; ?>
Date: <?php echo $info['date']; ?>
Gender: <?php echo $info['gender']; ?>
So for example visiting print.php?id=100 loads these values:
Id: 100
Name: John
Date: 2013-04-09
Gender: Male
Cisiting print.php?id=101 loads these values:
Id: 101
Name: Sarah
Date: 2013-04-09
Gender: Female
and so on.
When visiting each page I hit CTRL+P to print that information.
Instead of doing this manually (visiting each page and sending it to printer) I would like to have a script which grabs all the records of current date and sends it to my printer.
I'm not sure what you want to archieve, but I think you want a form like this:
<form method="get" action="">
<input type="text" name="id" value="100"/>
<input type="submit"/>
</form>
this would redirect the user to print.php?id=100 or with the value given in the textbox
EDIT:
What I read from the comments on the question is this what you want to archieve:
<form method="get" action="">
<input type="text" name="date" value="<?=date('yyyy-mm-dd');?>"/>
<input type="submit"/>
</form>
this would show a form with a textfield with the date of today printed in.
then you should make a query and fetch it like this:
$date = check_date($_GET['date']);
$query = 'SELECT * FROM values WHERE date="'.$date.'"';
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
// print row
}
}
the function check_date() is up to you to create, this checks if the text is a valid date to make sure nobody can hack your database
As I understand (it's not very clear) you want records for a specified date. Then you have to send a parameter for that date but not a single record id.
So, you can send a date with print.php?date=09-04-2013 for example and in you PHP script your query would be:
$date = $_GET['date']; // validate it
$sql = "SELECT * FROM table WHERE date = '$date'";
// fetch result
// print with a foreach loop as you like
Or if you want the current date only you don't need to pass a parameter at all. Just use CURDATE() if your date field contains only the date or NOW() if it contains date and time:
$sql = "SELECT * FROM table WHERE date = CURDATE()";
// fetch result
// print with a foreach loop as you like
More: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Still couldn't understand whether you have a problem with the HTML or with the PHP part.
If you are asking just for html forms and how to create a button you can check here:
http://www.w3schools.com/php/php_forms.asp
It's pretty simple to understand.

Categories