cakephp datetime input value not displayed - php

I've created an input-field for a datetime and I also get the value set, but I've got no value displayed:
HTML:
<input name="data[Date][dat_date_time]" dateformat="DMY" timeformat="24" type="datetime-local" value="2015-08-26 20:23:44" id="DateDatDateTime" required="required">
in Browser:
can anybody help me, why the date and the time is not displayed?

You need to add the letter T before your time:
<input name="data[Date][dat_date_time]" dateformat="DMY" timeformat="24" type="datetime-local" value="2015-08-26T20:23:44" id="DateDatDateTime" required="required">
More information: http://www.w3.org/TR/html-markup/input.datetime-local.html

Related

inservt database value into date form field

I've created a form so I can update a database table. Everything's working (text, file, select, radio) except my date form fields.
Empty, they show the default date placeholder text: mm/dd/yyyy. I can insert a value manually:
<input name="iu_received" type="date" class="form-control" id="iu_received" value="2022-09-20">
And that will show as 09/20/2022 on the page.
However, trying to insert a value from my database recordset doesn't do anything and the placeholder text still displays. For example:
<label class="form-label mb-0">Date Distributed:</label> <input type="date" class="form-control" name="iu_distrib" value="<?php echo $rsAppl->getColumnVal("iu_distrib"); ?>">
(Yes, the "iu_distrib" value is a mysql date: 2022-09-20.)
How can I make this work?

How to convert timestamp to html date input field value PHP

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

Filling the field of the datum form automatically with the date of today

I am building a web page for an admin dashboard using PHP and I have the following code, for the input date form and what I want is that if the "startdatum" is empty it must fill today's date in automatically.
<input type="date" class="form-control" placeholder="startdatum" id="startdatum" name="startdatum">
Here's how you can do it:
Btw, the <input type="date"> isn't supported on safari.
This example assumes that the input tag will always be empty when showing up, the user can later change the values.
<?php
$a = date("Y-m-d");
?>
<input type="date" id="startdatum" name="startdatum" value="<?php echo $a ?>">

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

populating datetime value from php into Jquery Moble text input datetime-local

I have read alot here, and figured out a lot of solutions from the posts. This is my first question. I hope I do well.
I am using the datetime-local input type from:
http: jquerymobile.com /demos/ 1.2.1 /docs /forms /textinputs/
I want to prefill it with the datetime from my database (using mysql and php)
Problem is I get a value in the field and no datetime picker or I get no value and the datetime picker will popup.
1.
I can't seem to find the documentation that describes how to prefill it. I am overlooking it?
2.
Can someone help me resolve this?
~=-=-=-=-=-=~
My test code is as follows.
<div data-role="fieldcontain">
<label for="datetime-l">Datetime local:</label>
<input type="datetime" name="datetime-l" id="datetime-l" value="<?php
$date1 = new Datetime($rrecord->strval('datenote'));
echo $date1->format(DateTime::ISO8601);
?>" />
</div>
<div data-role="fieldcontain">
<label for="datetime-2">Datetime local:</label>
<input type="datetime-local" name="datetime-2" id="datetime-2" value="<?php echo $rrecord->strval('datenote') ?>" />
</div>
The first field is filled with a date, but no datetime picker will pop up when touching it.
see linked screenshot -- http://picpaste.com/2013-09-12_21.45.11_1.png
The second field is empty, but a datetime picker pops up when touched.
see linked screenshot -- http://picpaste.com/2013-09-12_21.45.19.png
This is the debug code that echos the inputs. They show on the first screenshot.
<?php echo "debug datetime prefill from database"; ?>
<?php
echo "<br/>rrecord strval('datenote') = ", $rrecord->strval('datenote');
$date1 = new DateTime($rrecord->strval('datenote'));
echo "<br/> date1 format(DateTime::ISO8601) = ", $date1->format(DateTime::ISO8601);
?>
I was able to input a datetime using the phone. I looked at the output datetime format. The format was "Y-m-d\TH:i". Below I formatted the input date accordingly from PHP.
<div data-role="fieldcontain">
<label for="datetime-l">Date:</label>
<input type="datetime-local" name="datetime-l" id="datetime-l" value="<?php
echo date("Y-m-d\TH:i", strtotime($rrecord->strval('datenote')));
?>" />
</div>
This works.
$(document).ready(function() {
$("#datetime-1").val(<?php YOUR CODE ?>);
$("#datetime-2").val(<?php YOUR CODE ?>);
});
Its a bit hacky but it should work, just make sure the format of the date is what jq mobile is expecting.

Categories