Datetime value in the input field - php

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.

Related

Looking for help using php variable date in html

Hi im trying to use this date picker but i want it to allow user to only pick between two dates from php variables min is the earliest pickable date and max is the latest this is one variant of what i have tried but none have worked so far.
Any help is apreciated in advance
<form>
<label>
Choose your preferred party date (required, April 1st to 20th):
<input type="date" name="party" min="<?= echo date($minLanding)>" max="<?= echo date($maxLanding)>" required>
<span class="validity"></span>
</label>
<p>
<button>Submit</button>
</p>
</form>
You're using the shorthand echo syntax <?= and echo together. Use one or the other. Also note that the proper closing tag is ?>. And you'll likely have to provide a date format, though I'm not sure which off the top of my head.
<?= date('Y-m-d', $minLanding) ?>
Or:
<?php echo date('Y-m-d', $minLanding) ?>
If your values are already formatted dates:
$Today = date('y:m:d')
$minLanding = Date('y:m:d', strtotime('+14 days'));
$maxLanding = Date('y:m:d', strtotime('+9 months'));
Then just:
<input type="date" min="<?= $minLanding ?>" max="<?= $maxLanding ?>">
But note I think you'll have to use Y-m-d as the format instead of y:m:d.

php/html Why can't I echo a variable date in a date input field?

I try to show a standard date as value in my date type input field. For some reason they way I thought it would work doesn't work, already searched on google and stackoverflow for any solutions but couldn't really find anything. Maybe you guys have an idea? It is now like this , and I want to have it like this.
My code:
<?php $date = "1996-06-25";
$newDate = date("d-m-Y", strtotime($date)); ?>
<input type="date" name="date" value="<?php echo date($newDate);?>">
You need to change the date format, so try following
<?php $date = "1996-06-25";
$newDate = date("Y-m-d", strtotime($date)); ?>
<input type="date" name="date" value="<?php echo $newDate;?>">
Edited
Your current date format is not correct that's why it does not working.
#AniketSahrawat Thank you for letting me know. I modified my answer after your comment
The date format is wrong, try this out :
<?php
$date = "25-06-1996";
$newDate = date("Y-m-d", strtotime($date)); ?>
<input type="date" name="date" value="<?php echo $newDate; ?>">
Edit
Here's the proof that it works
The format like I said should be Y-m-d

Properly Format a datetime-local form input via echo from MYSQL Table

I'm attempting to echo the value of arrivalDateTimeDest into a form so that the user can edit this value. I've attempted to accomplish this a few ways, however, the value that's echo's always equals mm/dd/yyyy --:-- --. What am I doing wrong? My attempts are listed below.
<input type='datetime-local' id="arrivalDateTimeDest" name='arrivalDateTimeDest' value="<?php echo date_format($row['arrivalDateTimeDest'],'Y/m/d H:i:s')?>">
<input type='datetime-local' id="arrivalDateTimeDest" name='arrivalDateTimeDest' value="<?php echo date('Y/m/d H:i:s', $row['arrivalDateTimeDest'])?>">
<input type='datetime-local' id="arrivalDateTimeDest" name='arrivalDateTimeDest' value="<?php echo date('c', $row['arrivalDateTimeDest'])?>">
In light of the recent answers to this post, I've pinpointed the issue, however I cannot yet solve it. When input type = text, I have no issue echoing the value, however, I cannot do this when the input type is set to datetime-local.
Use strotitime before you change the format of date. Check below :
<?php $date = "2011-12-05 10:13 AM";?>
<input type='datetime-local' id="arrivalDateTimeDest" name='arrivalDateTimeDest' value="<?php echo date('Y-m-d\TH:i', strtotime($date)); ?>">
Yes This is very simple task to do ,
Convert time from database to timestamp .
$timestamp = strtotime($row['arrivalDateTimeDest']);
Then use date() function to format this date .. in a format which you want
echo date("Y-m-d", $timestamp);
use this where you want to echo the date ... :) :)
strtotime doesn't handle mm/dd/yyyy correctly, try this
echo DateTime::createFromFormat("m/d/Y H:i s", $row['arrivalDateTimeDest'])->format("y-m-d");
so in your code it should look like this:
<input type='datetime-local' id="arrivalDateTimeDest" name='arrivalDateTimeDest' value="<?php echo DateTime::createFromFormat("m/d/Y H:i:s", $row['arrivalDateTimeDest'])->format("y-m-d")?>">
make sure that this part: "m/d/Y H:i s" fits the format that you get from $row['arrivalDateTimeDest']

How to set a value for the input type 'datetime-local'?

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

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

Categories