简体   繁体   中英

How can I display current time in HTML input type 'time'?

I have tried it using PHP's 'time()' function but i am not getting my desired result on the frontend. Here is the line of code:

        <input type="time" class="form-control" id="select-time" value="<?php echo date('h:i A',time()); ?>" name="time" required>

Here is what it shows on the frontend: 在此处输入图像描述

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');

<input type="datetime-local"  value="<?php echo date('Y-m-d\TH:i:sP', $row['Time']); ?>" class="date" name="start" REQUIRED>

Use links for more info

https://www.php.net/manual/en/function.date.php

https://www.php.net/manual/en/class.datetime.php

You need to make sure that the format the date variable is printing out is valid for the HTML input you want to use.

If it is not (which is what I believe the issue is), then you will need to modify your date/time variable so that it prints in the correct format.

From the picture it looks like you simply want Hours:Minutes Seconds . Is this correct to assume?

<?php
//Optional if you wish to set your timezone
date_default_timezone_set('GMT');
 
// This would print the format 2021-09-07 11:04:48   
echo date("Y-m-d,h:m:s");

//You probably want to adjust it to something like this
echo date("h:m s");
?>

So to answer your question: Replace your PHP code ( <?php echo date('h:i A',time()); ?> ) with this, and it should be in the correct format.
<?php echo date("h:ms); ?>

if in database time stored like this "20:18" Simply Do the

<input type="time" class="form-control" name="activity_time" placeholder="Activity Time" value="<?php echo date('h:i') ?>" required />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM