首页 > 解决方案 > 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。

标签: phpwordpressformstimeformat

解决方案


这些方面的东西可能会有所帮助:

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

推荐阅读