In my code, here is how i convert html date field values to unix timestamp before inserting to MySQL db which works fine:
//HTML Code:
<div class="form-group">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" required/>
</div>
<div class="form-group">
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" required/>
</div>
//PHP Code to convert to Time Stamp:
<?php $post->unix_stamp = strtotime($_POST['dateFrom']);
$post->unix_stamp_ex = strtotime($_POST['dateTo']);
$post->save(); ?>
How do i convert the unix timestamp back to html input date value for editing? What i've tried:
<input type="date" name="dateTo" value="<?php echo date("Y-m-d\TH:i:s",$post->unix_stamp_ex); ?>" required/>
Where $post->unix_stamp_ex is the unix timestamp value. (doesn't work for me, shows dd/mm/yyyy instead). My problem is how to get the underlying value, to display in the date input so users can edit it
The reason your input is showing a placeholder 'dd/mm/yyyy' is because it cannot read the format you gave it. It can't work with time, so you need to remove it from your code:
value="<?php echo date('Y-m-d',$post->unix_stamp_ex); ?>"
This will show the date in the input.
Also, mind that the displayed value will differ from the actual value format, as per my comment and RoussKS' answer.
According to Mozilla Developer docs for input type date
The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.
[edit]
Just noticed the comment response, was typing as El_Vanja commented
[/edit]
Brother, I am able to convert the database string back to readable date time using the following method.
Solutions
$databaseTime = '2020-03-22 09:45:48';
$date = strtotime($databaseTime);
echo "DatabaseTime: " . $databaseTime . "<br>";
echo "Date in epoch: ".$date."<br>";
echo "Readable date: ".date("Y-m-d H:i:s",$date)."<br>";
Results
https://prnt.sc/rkmleq
I tried this:
<input type="datetime-local" value="<?php echo $row['Time']; ?>" class="date" name="start" REQUIRED>
How can I set the value of this input field with the data from the database?
It doesn't work!!
I need to make it possible to edit too.
Or should I use another type of input?
$row['Time'] is from the database!
I don't know exacly what is in $row['Time'] but it should be as follows:
Definition
A valid date-time as defined in RFC 3339 with these
additional qualifications:
the literal letters T and Z in the date/time syntax must always be uppercase
the date-fullyear production is instead defined as four or more digits representing a number greater than 0
Examples
1990-12-31T23:59:60Z
1996-12-19T16:39:57-08:00
Solution
To create RFC 3339 format in PHP you can use:
echo date('Y-m-d\TH:i:sP', $row['Time']);
or in another way:
echo date("c", strtotime($row['Time']));
or if you prefer objective style:
echo (new DateTime($row['Time']))->format('c');
In your code
So in your code it would look as follows:
<input type="datetime-local" value="<?php echo date('Y-m-d\TH:i:sP', $row['Time']); ?>" class="date" name="start" REQUIRED>
or
<input type="datetime-local" value="<?php echo date("c", strtotime($row['Time'])); ?>" class="date" name="start" REQUIRED>
Manual
More informations can be found here
PHP date Manual
PHP DateTime Manual
When submitting <form> using <input type="datetime-local">
the value format you will get is look like this.
2019-09-06T00:21
To set new value in your input type box.
You must use:
date('Y-m-d\TH:i', strtotime($exampleDate)) //2019-08-18T00:00
Solution Example:
$exampleDate = "2019-08-18 00:00:00"; //sql timestamp
$exampleDate = strtotime($exampleDate); //convert to unix timestamp
$newDate = date('Y-m-d\TH:i', $exampleDate); //format unix to date
or
$exampleDate = "2019-08-18 00:00:00";//sql timestamp
$newDate = date('Y-m-d\TH:i', strtotime($exampleDate));
If you don't use strtotime() you will get an error of
Notice: A non well formed numeric value encountered
**Tested**:
- $exampleDate = 2019-08-18 00:00:00 ;
- //Not Working - output(1970-01-01T01:33:39)
- <?php echo date('Y-m-d\TH:i:s', $exampleDate);?>
- //Not Working - output(1970-01-01T01:33:39+01:00)
- <?php echo date('Y-m-d\TH:i:sP', $exampleDate);?>
- //Not Working - output(2019-08-18T00:00:00+02:00)
- <?php echo date("c", strtotime($exampleDate));?>
- //Not Working - output(2019-09-23T19:36:01+02:00)
- <?php echo (new DateTime($row['Time']))->format('c');?>
- //Working Perfect - output(2019-08-18T00:00:00)
- <?php echo date('Y-m-d\TH:i:s', strtotime($exampleDate));?>
it's simple is that and working for me
first convert your php value to this format
<?php $datetime = new DateTime($timeinout[0]->time_in); ?>
then in value of html input element
use this format
<input type="datetime-local" id="txt_time_in" placeholder="Time In" name="timein" value = "<?php echo $datetime->format('Y-m-d\TH:i:s'); ?>" class="form-control" />
this will set your value to input element
The answer of Karol Gasienica is a great explanation but somehow did not work for me even in their replies
date('Y-m-d\TH:i:s', $row['Time']); //Gives me 1970-01-01 00:00
date('Y-m-d\TH:i:sP', $row['Time']); //Gives me no display
date("c", strtotime($row['Time'])); //No display too
What worked for me is this
$t = $row['Time'];
date('Y-m-d\TH:i:s', strtotime($t)); // This got it perfectly
However I still voted it up becauce of the explanation.
None of the above solutions worked for me as of 2019 using Google Chrome Version 78.0.3904.70 (Official Build) (64-bit)
What worked for me is.
<input type="datetime-local" value="2017-06-13T13:00">
As you can see the format is 2017-06-13T13:00 or Y-m-dTH:i.
As of PHP you can do like.
<input type="datetime-local" value="<?php echo Date('Y-m-d\TH:i',time()) ?>">
Hope this will save someone's time. :)
You can use
date('Y-m-d\TH:i'); //Example result: '2017-01-01T01:01'
if use \T instead of T (not working)
date('Y-m-dTH:i'); //Example result: '2017-01-01UTC01:01'
Better use timezone feature
function getCurrentDateTime(){
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT+6')); //Time Zone GMT +6
$dt= $date->format('Y-m-d\TH:i:s');
return $dt;
}
Try that. I get that in Bootstrap 4. (Google Translate: prueba con eso, a mi me sale con eso en Bootstrap 4. )
<input class="form-control" type="datetime-local" name="fecha_e" id="fecha_e" value="
<?php echo date('Y-m-d\TH:i', strtotime($data['fecha_e_tarea'])); ?>" required>
The simple way working for me is
<input type="datetime-local" value="<?= str_replace(' ', 'T', $date) ?>" name="date" required />
Easiest way! Here's how to set a value using php for input field with type="datetime-local"
setting value using php in type="datetime-local"
$date_now_time = date("Y-m-d H:i:s", strtotime('+5 hours'));
$from_for_val= date("Y-m-d\TH:i:s", strtotime($date_now_time));
Below is html form input field with type="datetime-local" and its showing how to set value inside input field
<input type="datetime-local" name="submit_from" value="<?=$from_for_val;?>" >
$tripid=$_REQUEST['tripid'];
$sql="SELECT * FROM tripdetails WHERE trip_id='".$tripid."'";
$trpresult=mysqli_query($connect,$sql);
if(mysqli_num_rows($trpresult)==1)
{
$trpdetails=mysqli_fetch_assoc($trpresult);
}
$trpstartdate = substr_replace($trpdetails['trip_start_date'],T,11,0);
$string = preg_replace('/\s+/', '', $trpstartdate);
This is Html part
<input type="datetime-local" name="trip_start_date" id="cal" value="<?php echo $string?>">
This will convert datetime from database to datetime-local
str_replace(" ","T",substr_replace($string ,"", -3))
Try it:
<input type="datetime-local" value="<?php $row['Time'] = preg_replace("/\s/",'T',$row['Time']); echo $row['Time']?>" class="date" name="start" REQUIRED>
for everybody on 2021 the working format is Y-m-d\TH:i
all other old format is just a hussel.
if you are using carbon library
\Carbon\Carbon::parse($data->chosen_date,$timezone)->format('Y-m-d\TH:i')
after that you can just pass to value like this
if are using ajax or fetch api
document.getElementById('chosen_date').value = data.chosen_date
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 }}">
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 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();