php - PHP Wordpress:将输入的时间存储为 HH:MM:SS.ms
问题描述
我在 wordpress 中有一个 PHP 页面,希望用户输入时间,格式为 HH:MM:SS.fff
请注意,'fff' = 秒的小数部分,到 3 位数。例如,1 小时、30 分钟、25 秒和 222 将是。01:30:25.222
以下过程适用于整数/浮点数,但我需要为此示例设置一段时间的格式。
在顶部,我获取了 user_ID 并为现有设置了一个变量。
$user_id = get_current_user_id();
$existing_100MRow = ( get_user_meta( $user_id, 'user-100MRow', true ) ) ? get_user_meta( $user_id, 'user-U100MRow', true ) : '';
然后我有一个带有以下 HTML 的表单。我试图找到解决方案,这就是为什么它包含时间类型。
<label for="user-U100MRow"><strong>100M ROW</strong>
<input id="user-U100MRow" type="time" value="00:00:00" step="1" name="user-U100MRow" value="<?php echo $existing_U100MRow; ?>">
</label>
然后我从输入中设置:
$_100mRow = ( ! empty( $_POST['user-_100MRow'] ) ) ? floatval( $_POST['user-_100MRow'] ) : '';
然后更新用户:
wf_insert_update_user_meta( $user_id, 'user-100MRow', $_100MRow);
我希望用户能够以 HH:MM:SS.fff 的格式输入时间,以便更新并显示默认值。
一世
从那以后,我尝试了以下方法:
$_100mRow = (!empty($_POST['user-_100MRow'])) ? MICROSECOND(DATE_FORMAT(( $_POST['user-_100MRow'] ),'%H:%i:%s.%f')) : '';
仍在挣扎。在数据库中显示 NULL。
解决方案
这些方面的东西可能会有所帮助:
switch(!empty($_POST['user-_100MRow'))
{
case true:
$dt = new DateTime($_POST['user-_100MRow']);
break;
default:
$dt = new DateTime();
}
$tformat = $dt->format('H:i:s.u');
update_user_meta(
$user_id,
'user-_100MRow',
$tformat
);
推荐阅读
- elasticsearch - Kafka Connect Elasticsearch sink 没有文档被索引
- reactjs - Graphql:Typescript + Jest + Relay + Jest 似乎无法集成在一起
- php - 如何在php中格式化打印输出
- c# - 将位置设置为底部时不显示 TabbedPage 图标
- google-colaboratory - 在 Google Colaboratory 中长时间运行会话结束后,如何用剩余的 epoch 训练模型。?
- java - Hibernate EntityManager#getResultList 挂起,没有任何错误
- html - HTML文件的图像未在ios应用程序中加载
- javascript - window.parent.postMessage 不适用于 iframe 中的 scr="'data:text/html;charset=utf-8"
- angular - 如何在 Ionic Ecommerce App 中应用产品搜索
- dns - 如何强制用户从特定的办公地点到特定的 ADFS 服务器