【问题标题】:PHP Wordpress: Store time from input as HH:MM:SS.msPHP Wordpress:将输入的时间存储为 HH:MM:SS.ms
【发布时间】:2020-03-25 22:02:14
【问题描述】:

我在 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 的表单。我试图找到解决方案,这就是为什么它包含 Time 类型。

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

仍在苦苦挣扎。在 DB 中显示 NULL。

【问题讨论】:

  • 试试只用PHP的日期函数date("H:i:s.u", strtotime($_POST['user-_100MRow']));

标签: php wordpress forms time format


【解决方案1】:

类似的事情可能会有所帮助:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2010-11-23
    • 2013-04-20
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多