PHP. How to assign current date and time to an input field - php

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();

Related

php format field datetime-local in to php

i have this input which i take like this
<input type="datetime-local">
which i am inserting in the db successfully. In the database it stores as such in my table
2021-05-17 23:00:00
but then i am trying to display it in my page like so
<input type="datetime-local" value="<?php echo $date_time; ?>" >
but it gives me a blank field
but when i inspect it shows my the correct value in the value field
get datetime from database using DATE_FORMAT()
SELECT *, DATE_FORMAT(datetime, '%Y-%m-%dT%H:%i') AS custom_date
and after fetching data in which variable your data exist use that in your input value
<input type='datetime-local' name='datetime' value='<?php echo $row['custom_date'] ?>' >

get the data to datetime-local from php

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 }}">

date from database to html datepicker showing 01/01/1970

I want to auto get auto date value from database to html form on datepicker, I tried this one but getting 01/01/1970 instead of that date stored in database.
<input name="last_date" type="date" value="<?php echo strftime('%Y-%m-%d', strtotime($query[lastdate]->startdate)); ?>" />
while when I try , <? echo $query['lastdate']; ?> it showing correct date from database

submit the current date alongside the rest of the form

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.

How to assign default value current date and time to input field

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

Categories