ACF +时间选择器 - 不能将时间返回为字符串值

时间:2018-02-09 09:58:46

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

好的,我正式在智慧结束,希望有人可以协助我的代码。刚刚开始涉足PHP和ACF,我试图在自定义的帖子类型“事件”中提取自定义值,其中包含各种信息。

我想说的是:

如果事件是多天事件,由“event_type”字段中的值“multi”标注,则“event_start”“event_end”日期,以便读取“EVENT_START到EVENT_END”

如果“event_type”的值不是“multi”(它将是“单个”),那么我想显示“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;
} 

2 个答案:

答案 0 :(得分:0)

好的,所以我能够在很大程度上解决这个问题,但仍然存在一个问题,即第一个日期的日期时间恢复为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>

答案 1 :(得分:0)

最后更新,代码工作,我只需要从一个组中的嵌套位置删除转发器,以便正确显示字段并转换UNIX时间。