I'm trying to get the current date to be submitted to the database without the user entering a date.
I currently have the following php code
<?php $currentDate = time(); ?>
And then within the form ive assigned the php code as a value to a hidden input.
<input type="hidden" name="mDateJoined" value="$_POST['currentDate']">
this doesnt seem to work though? where am i going wrong?
You dont have to send the date through the form. In the php code that processes your script, just use the now() function in the query
Like:
INSERT into `users` (`name`,`date`) VALUES('$name',now())
In your database, your date column must be of date type.
OR
date_default_timezone_set('UTC');
$date = date('Y-m-d');
INSERT into `users` (`name`,`date`) VALUES('$name','$date')
If these both statements are on the same page just change:
<input type="hidden" name="mDateJoined" value="<?php echo $currentDate; ?>">
This will print the value of $currentDate in your hidden field .
You need to set current date exactly to hidden field:
<input type="hidden" name="mDateJoined" value="<?php echo time();">
And when handling the submitted data you can do:
$currentDate = (new \DateTime())->setTimestamp((int) $_POST['mDateJoined']);
I guess
<?php $currentDate = time(); ?>
and
<input type="hidden" name="mDateJoined" value="$_POST['currentDate']">
are on same page. Try this instead of above code lines:
<?php $currentDate = date('Y-m-d'); ?>
<input type="hidden" name="mDateJoined" value="<?php echo $currentDate; ?>">
I will prefer do not pass the date as hidden field in the form and use <?php $currentDate = date('Y-m-d'); ?> at the place where you are inserting the date into the database.
Related
I have a problem echoing the DateTime in the input field with PHP. Now I cannot echo the selected DateTime value in the input field.
Below is my sample coding, I use below method cannot echo selected value:
<?php
$datetime ="2021-06-04 09:13:00";
?>
<input type="datetime-local" class="form-control" id="datetime" name="datetime" value="<?php echo date('Y-m-d\TH:i:s', strtotime($datetime));?> ">
The result shows me like below picture:
This is my online working sample code : https://paiza.io/projects/D-J1uBPeHJuL8jCOhToRQQ
Hope someone can guide me on how to solve this problem. Thanks.
You may follow like this
<?php
// Ajmal Praveen
$datetime ="2021-06-04T09:13:00";
?>
<input type="datetime-local" value="<?php echo $datetime; ?>">
It will work. You need to Include the T, between date and time
PHP To Modify Existing Date time from Database using HTML5 Datetime-local
I written this function mainly to get work the HTML5 Datetime, it can be used with any of script.
<?php
// Code by Ajmal Praveen
$time = date("Y-m-d H:i:s");
// Used Current Date time
$GetDate = date("c", strtotime($time));
list($Date) = explode('+', $GetDate);
$GetDate = $Date;
// Code by Ajmal Praveen
//Output
?>
<input type="datetime-local" value="<?php echo $GetDate; ?>">
If the code is useful Do a Upvote. Thank you.
I am using a date input type to select a date in my PHP project.
Now everytime I submit the form I want to display the date in the input that has been selected.
This is what I've tried:
<?php $date = $_GET['date']; ?>
<input placeholder="<?php $date ?>" type="date" name="date" id="date" />
But unfortunately this doesn't seem to work.
Anyone has any idea if my goal is possible?
Many thanks in advance!
You can't set a placeholder to <input type="date"/>. See the following information from HTML specification:
The following content attributes must not be specified and do not apply to the element: accept, alt, checked, dirname, formaction, formenctype, formmethod, formnovalidate, formtarget, height, inputmode, maxlength, minlength, multiple, pattern, placeholder, size, src, and width.
https://html.spec.whatwg.org/multipage/forms.html#date-state-%28type=date%29
You have to set the date as value like the following:
<?php $date = $_GET['date']; ?>
<input type="date" name="date" id="date" value="<?= $date ?>" />
An example code:
<input placeholder="2011-01-01" type="date" id="date"/>
<input value="2011-01-01" type="date" id="date"/>
You need to echo that variable, or else nothing will display.
Like this:
//Shorthand
<?=$date ?>
//Not shorthand with print
<?php print $date; ?>
//Not shorthand with echo
<?php echo $date; ?>
People would probably prefer echo instead of print.
You need to use the variable inside value
<?php
$date = isset($_GET['date']) ? $_GET['date'] : '';
?>
<input value="<?php echo $date; ?>" type="date" name="date" id="date" />
You need check the form submission data first, before fetching the data in PHP. Request you to check form data and submission method.
I would like to suggest use $_REQUEST['date'] instead of $_GET.
If this does not work, let us see your form code?
In addition to the answers where the echo was mentioned as solution, you should add the following after $date:
<?php $date = isset($_GET['date']) ? $_GET['date'] : false; ?>
When $date is not found, this will result in not giving a warning message. If you don't, and the $_GET['date'] can't be found you'll get a undefined variable warning message..
I have a datatype "TIMESTAMP" in database. And I pass the value by using "datetime-local" attribute in html.
<input type="datetime-local" id="start_time" class="form-control" name="start_time"/>
this works fine and i can upload the date and time to database. But when i retrieve the data form database for editing, it doesn't work.
PHP Code
$query="SELECT * FROM hire WHERE hire_id='$id'";
$query_run=mysqli_query($con,$query);
$row=mysqli_fetch_array($query_run);
HTML Code
<input type="datetime-local" value="<?php echo $row['start_time']; ?>" />
The above php within the html shows nothing as a result. What is the fault here?
Because you have the input type set to 'datetime-local'. You have to have the date set to the right format in order for it to show up in the input.
Change your start_time value to be in this format.
$date = date("Y-m-d\TH:i:s", strtotime($row['start_time']));
Then in your input field echo out $date
<input type="datetime-local" value="<?php echo $date; ?>" />
first of all if you should get date from mysql then convert that string to date using strtotime function after that change the format of date and set it at value attribute of input form control haivng type="datetime-local".
$string_to_date=$d=strtotime($date_fromdatabase);
$new_date=Date('Y-m-d\TH:i',$string_to_date);
set as a value attribute of input element in laravel 6
<input type="datetime-local" class="form-control" value="{{ $new_date }}">
How to assign default value current time stamp to input field then i want to insert into db as hidden.
<input
name="status" id="status"
type="hidden"
value="<?php echo CURRENT_TIMESTAMP ?>"
/>
</td> </tr>
just tell me how to assign default value (current taimestamp).
because we can't add columns in db more than 1 as current time stamp i have already 1 column current time stamp in db table so i need 1 more that's why i want to add as hidden.
For hidden field you can use php function date()
$data['column_name'] = date('Y-m-d H:i:s');
$data['other_column1'] = 'other value 1';
$data['other_column2'] = 'other value 2';
$data['other_column3'] = 'other value 3';
$this->db->insert('tabel_name',$data);
Try like this
<input type="hidden" value="<?php echo time();?>" name="my_unique">
that's it
you will get current time stamp using : time();
So : <input name="time" type="hidden" value="<?php echo time();?>" >
Ref: http://php.net/manual/en/function.time.php
<?php echo date("Y/m/d h:i:s a"); ?>
is it ok on this input
How to assign default value current time stamp to an input field.
Then I want to insert into db as hidden.
How can I assign a default value (current taimestamp)?
<input name="status" id="status" type="hidden"
value="<?php echo CURRENT_TIMESTAMP ?>"/>
Use value= <?php echo date('Y-m-d H:i:s'); ?>
Set the date format as per your need.
<input type="hidden" name="tstamp" value="<?php echo time(); ?>" />
You could do it in the backend, just by using a default value to the table column.
UPDATE <table_name> SET <column_name> = NOW();