0

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: enter image description here

Dexter
  • 7,911
  • 4
  • 41
  • 40
mkz
  • 119
  • 1
  • 8
  • 2
    So what is your desired result on the front end, and what are yo actually seeing on the front end – RiggsFolly Sep 07 '21 at 09:19
  • 2
    **NOTE** the `time()` in `date('h:i A',time());` is irrelevant. If left out it will do exactly that by default – RiggsFolly Sep 07 '21 at 09:20
  • what are you seeing and what do you want to see? – DevWithZachary Sep 07 '21 at 09:22
  • @RiggsFolly I want the current time picked and shown in the input field on the frontend just as the page loads. – mkz Sep 07 '21 at 09:23
  • @RiggsFolly-- yes I expected the same, but nothing shows up except the default format '--:-- --' – mkz Sep 07 '21 at 09:27
  • 3
    That is because you are setting it in a wrong format. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#time_value_format – CBroe Sep 07 '21 at 09:27
  • Use `H` (capital letter). This outputs 24hr format and you will get the desired result. – Dexter Aug 06 '23 at 14:32

4 Answers4

2

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

Asif
  • 166
  • 9
2

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:m s); ?>

Brad Andrews
  • 106
  • 1
  • 1
  • 11
  • Please mark this as the solution if you find it to have helped otherwise if you are still having problems with this, please leave a comment with more info about what is going wrong and I will attempt to help you. :) – Brad Andrews Sep 07 '21 at 10:09
1

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 />
Sanmit Pawar
  • 211
  • 3
  • 10
0

@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)

Using h:i A (doesn't work ✘)

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

Using h:i (doesn't work ✘)

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

H:i (works ✔)

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

Dexter
  • 7,911
  • 4
  • 41
  • 40