I've researched a lot, but nothing helped me. I have 2 views: one for sign up and another one to edit your infos, if you forgot something.
Let's say I wrote in the date label the following month and year: 98/9889 (yeah, i'm retarded). It will shows correctly, but when I get my infos to edit it, it shows 31/1219, but in my database it's still 98/9889. Any idea?
SIGN UP VIEW:
<label for="antitetanica">Data da última dose antitetânica:</label>
<input type="text" id="antitetanica" name="antitetanica" value="echo date("m/Y", strtotime('antitetanica'))"/><br /><br>
EDIT VIEW:
<label for="antitetanica">Data da última dose antitetânica:</label>
<input type="input" name="antitetanica" id="antitetanica" value="<?php echo date("m/Y", strtotime('antitetanica')) ?>"/><br /><br>
Btw i was using DATE type in my database, but now i'm using varchar 255.
EDIT: now using the DATE type in my db
EDIT 2: actual code looks like (sorry, first time working with DateTime):
SIGN UP VIEW:
<input type="input" id="antitetanica" name="antitetanica" value="
<?php $date = date_create_from_format( 'm/Y', $antitetanica )->format('m/Y'); ?>" /><br /><br>
EDIT VIEW:
<label for="antitetanica">Data da última dose antitetânica:</label>
<input type="input" name="antitetanica" id="antitetanica" value="<?php $date = new DateTime( $antitetanica ); echo $date->format('m/Y'); ?>" /><br /><br>
Because strtotime doesn't recognise 'antitetanica' ('tetanus') as a valid date:
PHP strtotime
Even assuming that you've missed a '$' off the start of that string and are actually attempting to feed strtotime a string value which has come from your database - this is not what it is for. You can't feed it 98/9889 and hope to get a valid date.
EDIT
You had the right idea - but strtotime was the wrong way to create your date object.
If your input is "03/2013", you need to convert that into a valid date object that PHP can work with. You do that with DateTime::createFromFormat();
$date = DateTime::createFromFormat( 'm/Y', '03/2013' ); //<< Object-Oriented Style
or
$date = date_create_from_format( 'm/Y', '03/2013' ); //<< Procedural Style
Once you have a date object, you can then output it in whatever format you like:
$date->format('m/Y'); //<< Object
or
date_format( $date, 'm/Y' ); //<< Procedural
So - in your original question (storing the date as a string in the database), it would look like:
echo date_create_from_format( 'm/Y', $antitetanica )->format('m/Y');
But now that you have an actual DATE object in your database, that statement looks something like:
echo date_create_from_format( 'Y-m-d', $antitetanica )->format('m/Y');
Which is NOT the ideal way to do it ... but I'm painting a picture for you here.
A better way to work with dates now that you have actual DATES in your database, is with DateTime
When you retrieve your data from the database, you can now do something like:
$date = new DateTime( $antitetanica ); //<< (where $antitetanica = "2013-03-01")
echo $date->format('m/Y'); //<< "03/2013"
SO - How to get the date from a user in the first place?
There are several ways you could do this. You could get the user to enter the date using multiple 'select' form elements, or multiple 'text' form elements, or a single 'text' form element - and each of these ways would need to be handled differently in PHP.
Let's say you decide to have the user enter the date "03/2013" into a single text field. Over on the server side of things, you'd need to follow exactly the same steps as above - that is:
Convert user input to a valid DateTime object
Format and insert the date into your database
So:
$date = DateTime::createFromFormat( 'm/Y', $userInput );
INSERT INTO myTable ( antitetanica ) VALUES ( '$date->format('Y-m-d')' ) //<< Pseudocode
As 2 text input fields, that might look like:
$date = DateTime::createFromFormat( 'm/Y', $userMonth . '/' . $userYear );
INSERT INTO myTable ( antitetanica ) VALUES ( '$date->format('Y-m-d')' ) //<< Pseudocode
Related
I need to change the pattern of the input date field to Day.Month.Year, is it possible?
<input type="date" name="datum" value="datum"/>
Currently it looks like this:
but if I submit the form and catch the value in PHP then the value is stored like this "2022-02-17"
I found this section in the firefox documentation, but it does not provide any example.
You could correct the date once you catch the value with your PHP handler.
$date = $_POST['datum'];
$date = date("Y-m-d",strtotime($date));
To show date in d.m.Y format in HTML page, again convert it into
$date = date("d.m.Y",strtotime($date));
Here is an input form which passes date type to intermediate.php from a form.
<form action="intermediate.php" method="post">
<input type="date" name="vazhipadudate1" />
</form>
How can i get the picked date i tried this code snippet .It echos as 1970-01-01
Php code snippet.
$date='vazhipadudate1';
$time = strtotime($_POST["$date"]);
$storecart[$i]['date'] = date('Y-m-d', $time);
echo "Selected Date is..........".$storecart[$i]['date'] ;
The output i am getting as
Selected Date is..........1970-01-01
After reproducing the problem,
I think the problem is probably because your submitting the form without passing any value to input.
As The input has no def value. So it, POSTs empty string.
And when you pass the empty string to strtotime that returns false
Amd, again when you pass it to date that returns 1970-01-01 as (int) FALSE -> 0
So you should test the POST before processing it. Something like this to check POSTed data,
!empty( $_POST["vazhipadudate1"] )
# AND/OR,
$time = strtotime($_POST["$date"]);
if (time === false) /*TakeAppropriateAction*/;
I want to always show the previous third in the input and for this I do it this way:
$dia = new DateTime();
$dia->modify( 'previous tuesday' );
$terca = date($dia->format('d-m-Y'));
Then I want to show the variable $terca in the value of an input type date, but it does not show, only if it is datetime:
<td style="float:center"> <input type="date" name= "data" value="<?php echo $terca?>"></td>
Whenever I run the page I get this warning:
The specified value "19-02-2019" does not conform to the required
format, "yyyy-MM-dd".
I'm trying in this way date ('yyyy-MM-dd', strtotime ($terca)); but I still have the same problem
Change format('d-m-Y') to format('Y-m-d')
$dia = new DateTime();
$dia->modify( 'previous tuesday' );
$terca = date($dia->format('Y-m-d'));
Then try :
<input type="date" name= "data" value="<?php echo $terca?>">
The error-message tells you the problem right away:
The specified value "19-02-2019" does not conform to the required format, "yyyy-MM-dd"
Just change the format (put Y to the front):
$terca = date($dia->format('Y-m-d'));
Should solve your problem.
The error message is telling you that the string you've output into the value of the input box is not valid.
As is documented, the value of a "date" input must always be specified in yyyy-mm-dd format. This is irrespective of the format in which is it displayed to the user (which is chosen based on the user's browser locale settings).
You can fix it by using the correct PHP date format string, like this:
PHP:
$dia = new DateTime();
$dia->modify( 'previous tuesday' );
$terca = $dia->format('Y-m-d');
<input type="date" name="data" value="<?php echo $terca?>">
Runnable demo of PHP: http://sandbox.onlinephpfunctions.com/code/7045183fd11b5a2e29d5d9fa80f0910cad18d671
Runnable demo of HTML using the string output by the PHP: https://jsfiddle.net/0mqokve6/
P.S. since $dia is already a DateTime object, and format() already outputs a string, wrapping it in the date() function is redundant.
P.P.S. The reason the date control doesn't allow strings in other formats there is always a potential for ambiguity. e.g. dd-mm-yyyy can be impossible to distinguish from mm-dd-yyyy in many cases, such as 01-03-2019 as just one example. In that scenario it's impossible for the browser to know what the intended date was. yyyy-mm-dd is unambiguous and therefore always used to convey the actual value. The user can then be shown it in the format they're more culturally familiar with.
I'm creating a simply form to insert event data into my database.
Form:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Event Date/Time: <input type="datetime-local" name="event_date">
Event Title: <input type="text" name="event_title">
Event Details:
<textarea id="tiny_mce" name="description" rows="8"></textarea>
<input type="submit" value="Upload">
</form>
Action: upload.php
// new data
$event_date = $_POST['event_date'];
$event_title = $_POST['event_title'];
$description = $_POST['description'];
// query
$addevent = DB::getInstance()->insert('events', array(
'event_date ' => $event_date,
'event_title' => $event_title,
'description' => $description,
));
echo "<pre>".
var_dump($addevent);
echo "</pre>";
If I do a var_dump on each value in my upload script, I get the value to return correctly.
When I do a var_dump($addevent); it comes back as bool(false) and nothing gets added to my table.
If I remove <input type="datetime-local" name="event_date"> & $event_date = $_POST['event_date']; Everything works fine.
So I narrowed down that the problem must be in the datetime.
How should my DATETIME be setup properly in my database.
Right now I have event_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
Obviously this is not working... Any suggestions on what it should be?
Assuming why removing event_date works would be due to your DB class finds no error in casting or format and allows the save to continue as opposed to sending malformed data.
DATETIME expects a string like date("Y-m-d H:i:s")
If you can't verify that format is coming in to your script, you can allow PHP to parse and format it for you like: date("Y-m-d H:i:s",strtotime($event_date))
May be this is not the answer you want. But I want to suggest you how to debug data POST in upload.php.
By this your statement:
When I do a var_dump($addevent); it comes back as bool(false)
You should debug only $_POST['event_date']; by die($event_date);. So by debugging only event_date like this, you can know what is the value of $_POST['event_date'];
// new data
$event_date = $_POST['event_date'];
die($event_date);
//this will show you the value of $_POST['event_date'];
$event_title = $_POST['event_title'];
$description = $_POST['description'];
// query
$addevent = DB::getInstance()->insert('events', array(
'event_date ' => $event_date,
'event_title' => $event_title,
'description' => $description,
));
echo "<pre>".
var_dump($addevent);
echo "</pre>";
The problem was here:
// query
$addevent = DB::getInstance()->insert('events', array(
'event_date ' => $event_date,
'event_title' => $event_title,
'description' => $description,
There is a space in between 'event_date ' ... should be no space like this 'event_date' ... Bonehead Mistake.
I ended up inserting my datetime-local into my table with a timestamp option like this so I could output it where I wanted and style the the date and time to my liking with css. Here is what my table looked like:
Thanks for all the help, I still learned some useful info.
The way I see this: Do you want to use the server CURRENT_TIMESTAMP to have the time the client hit "Submit". Or do you want to give the client the freedom to enter the time and date. You are trying to do both in your code.
I am going to guess you want the CURRENT_TIMESTAMP you set up on the MySQL database. Drop the:
In the SQL code change the $event_date VALUE to (DEFAULT).
For example, I have a textbox in which I want to set by default a value from database. I can do it like this:
<input type="text" name="abcd" value="<?=#$result['xyz'];?>"/>
Ok, it works for this type of field.
But, I want to do it for a date-time field like this:
$datetime = $result['datetime'];
//already converted to format("d/m/Y, h:i:s A")
<input type="datetime-local" value="<?=#$datetime?>" name="abc"/>
I've already tried converting the database date-time format to fit this format. No result.
Ideas? Thanks!
Try using:
$datetime = new DateTime($result['datetime']);
<input type="datetime-local" value="<?= $datetime->format('Y-m-d H:i:s'); ?>" name="abc" />
This will always try to format your date and time to a correct format. If this fails, PHP will throw an exception letting you know what's wrong.