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

后端 未结 10 1271
猫巷女王i
猫巷女王i 2020-12-25 11:59

I tried this:

\" class=\"date\" name=\"start\" REQUIRED>

H

相关标签:
10条回答
  • 2020-12-25 12:16

    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

    0 讨论(0)
  • 2020-12-25 12:18

    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.

    0 讨论(0)
  • 2020-12-25 12:20
    $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?>">
    
    0 讨论(0)
  • 2020-12-25 12:23

    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'
    
    0 讨论(0)
  • 2020-12-25 12:27

    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);
    $newDate = date('Y-m-d\TH:i', $exampleDate);
    

    or

    $exampleDate = "2019-08-18 00:00:00";//sql timestamp
    $newDate = date('Y-m-d\TH:i', strtotime($exampleDate));
    

    If you dont 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));?> 
    
    0 讨论(0)
  • 2020-12-25 12:36

    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

    0 讨论(0)
提交回复
热议问题