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

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.

Related

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

HTML/PHP display current date in html date input

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..

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

cakephp datetime input value not displayed

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

Categories