teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2020/05/10 02:32

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -7,7 +7,25 @@
7
7
  $time2 = "";
8
8
  ?>
9
9
  <input type="time" name="time" value="<?php echo ($time) ?? "" ?>">
10
- <input type="time" name="time" value="<?php echo ($time2) ?? "" ?>">
10
+ <input type="time" name="time2" value="<?php echo ($time2) ?? "" ?>">
11
11
  ```
12
12
 
13
- [Null 合体演算子](https://www.php.net/manual/ja/language.operators.comparison.php#language.operators.comparison.coalesce)
13
+ [Null 合体演算子](https://www.php.net/manual/ja/language.operators.comparison.php#language.operators.comparison.coalesce)
14
+
15
+ `1023`ならこんな感じで、「時間の型が正しいか」関数を作ってチェック。
16
+ ```php
17
+ <?php
18
+ function timeFormat($t):string
19
+ {
20
+ if(date("Hi",strtotime($t)) === $t){
21
+ return date("H:i",strtotime($t));
22
+ }
23
+ return "";
24
+ }
25
+
26
+ $time = "1023";
27
+ $time2 = "";
28
+ ?>
29
+ <input type="time" name="time" value="<?php echo timeFormat($time) ?>">
30
+ <input type="time" name="time2" value="<?php echo timeFormat($time2) ?>">
31
+ ```

1

修正

2020/05/10 02:32

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -1,11 +1,13 @@
1
1
  DBから取得した情報を$timeにおさめてるならisset($time)は常にtrueになりませんか。(空とnullは別物です)
2
2
 
3
+ DBのデータの形式次第ですが、そもそも`10:23`のように入っているなら下記だけで良いと思います。
3
4
  ```php
4
5
  <?php
5
- $time = date("H:i");
6
+ $time = "10:23";
6
-
7
+ $time2 = "";
7
8
  ?>
8
9
  <input type="time" name="time" value="<?php echo ($time) ?? "" ?>">
10
+ <input type="time" name="time" value="<?php echo ($time2) ?? "" ?>">
9
11
  ```
10
12
 
11
13
  [Null 合体演算子](https://www.php.net/manual/ja/language.operators.comparison.php#language.operators.comparison.coalesce)