【问题标题】:ACF + Time Picker - Can Not Return Time As String ValueACF + 时间选择器 - 不能将时间作为字符串值返回
【发布时间】:2018-02-09 09:58:46
【问题描述】:

好吧,我正式束手无策,希望有人可以帮助我编写代码。刚开始涉足 PHP 和 ACF,我试图从自定义帖子类型“事件”中提取自定义值,其中包含各种信息。

我想说的是:

如果事件是多天事件,由 "event_type" 字段中的值 "multi" 表示,然后是 "event_start"“event_end” 日期被提取和格式化,以便读取 “EVENT_START 到 EVENT_END”

如果 "event_type" 的值不是 "multi"(应该是“single”),那么我想显示 "event_start" 日期,然后分别存储在时间选择器对象 "event_start_time""event_end_time" 中的 from 和 to 时间。

我已经得到了它的日期部分,以及基于价值的选择部分,但对于我的生活,我无法让时间出现。

代码如下:

$event_type = get_field('event_type');
$start_date = get_field('event_date');
$end_date = get_field('event_end_date');
$end_time = get_field('event_end_time');
$str_start_time = date("g:i a", strtotime($start_time));
$str_end_time = date("g:i a", strtotime($end_time));
$str_start_date = date("F j, Y", strtotime($start_date));
$str_end_date = date("F j, Y", strtotime($end_date));

if ($event_type=='multi'){
    echo ($str_start_date . " to " . $str_end_date);
}
else{
    echo $str_start_date . " from " . $str_start_time . " to " . $str_end_time;
} 

【问题讨论】:

    标签: php advanced-custom-fields php-7.1


    【解决方案1】:

    好的,所以我能够在很大程度上弄清楚这一点,但仍然存在第一个日期的日期时间恢复为 UNIX 时间和 1970 年作为日期的问题。这是更新的代码:

    <div class="row" align="center"><h3>WHEN</h3></div>
    <?php
    $event_type = get_field('event_type');
    $start_date = get_field('event_start');
    $start_date = date("F j, Y g:i a", strtotime($start_date));
    $start_date_only = new DateTime($start_date);
    $start_date_only = $start_date_only->format('F j, Y');
    $start_time_only = new DateTime($start_date);
    $start_time_only = $start_time_only->format('g:i a');
    $end_date = get_field('event_end_date');
    $end_date = date("F j, Y g:i a", strtotime($end_date));
    $end_date_only = new DateTime($end_date);
    $end_date_only = $end_date_only->format('F j, Y');
    $end_time_only = new DateTime($end_date);
    $end_time_only = $end_time_only->format('g:i a');
    
    ob_start(); //Start output buffer
    echo $start_date_only . " to " . $end_date_only;
    $start_end_date = ob_get_contents(); //Grab output
    ob_end_clean(); //Discard output buffer
    ob_start(); //Start output buffer
    echo $start_date_only . " from " . $start_time_only . " to " . $end_time_only;
    $date_and_time = ob_get_contents();
    ob_end_clean(); //Discard output buffer
    ?>
    
    <div class="row" align="center"><h3><?php echo "$start_end_date"; ?></h3></div>
    <div class="row">
    <div class="row" align="center"><h3>WHERE</h3></div>
    <div class="row" align="center"><h3><?php the_field('event_location_name') ?><br><?php the_field('event_location_address'); ?></h3></div>
    </div>
    

    【讨论】:

      【解决方案2】:

      最终更新,代码有效,我只需要将转发器从组中的嵌套位置移除,以便字段正确显示并转换 UNIX 时间。

      【讨论】:

        猜你喜欢
        • 2015-01-31
        • 2011-04-06
        • 2014-01-05
        • 2023-03-21
        • 1970-01-01
        • 2021-12-11
        • 2017-12-16
        • 2018-10-28
        • 1970-01-01
        相关资源
        最近更新 更多