Reputation: 139
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:
Upvotes: 0
Views: 6192
Reputation: 9324
@RiggsFolly is correct, the input time from the database is irrelevant because the date()
function is to convert the string to date + format the output.
Let's say the current time is 7:30 pm (night)
PHP echo
Because you specified AM
it is showing the AM in the output
$t = date('h:i A');
echo $t;
// output: 7:30 pm
HTML date
input
The same AM
will not work inside the date input
<input type="time" value="<?php echo date('h:i A') ?>">
<!-- output: --:-- -- -->
PHP echo
How about we use without AM
or PM
. just plain time not provide the AM
PM
. Probably if you are following 24hrs format then you can consider this as AM
$t = date('h:i');
echo $t;
// 7:30
HTML date
input
<input type="time" value="<?php echo date('h:i') ?>">
<!-- output: 07:30 AM (the AM and PM doesn't work here, the reason why it displays AM is because that is the first thing in the date dropdown list) -->
PHP echo
You can try the H
. This is for 24hr format.
$t = date('H:i');
echo $t;
// 19:30
HTML date
input
No need to specify the A
, since it is 24hrs format, the input automatically detects and displays the correct time.
<input type="time" value="<?php echo date('H:i') ?>">
<!-- output: 7:30 pm -->
Here's a simple explanation: https://stackoverflow.com/a/51515814/5413283
Upvotes: 0
Reputation: 243
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 />
Upvotes: 2
Reputation: 98
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 ( ) with this, and it should be in the correct format.<?php echo date('h:i A',time()); ?>
<?php echo date("h:m s); ?>
Upvotes: 2
Reputation: 334
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
Upvotes: 2