如何比较循环值之内的当前日期?

发布于 2025-02-01 22:18:32 字数 2361 浏览 4 评论 0原文

我有一些活动日期,例如以下屏幕截图,为此我正在使用ACF中继器。

我有一个功能,我试图将票证注册开始日期与当前日期进行比较。

function santy_shortcode_race_categories_event_post(){
    date_default_timezone_set('Asia/Kolkata');
    $result = '<div class="table-responsive">
                <table class="table table-bordered" style="text-align:center; background-color:#fff;">
                <thead>
                    <tr class="table-head">
                        <th>Package</th>
                        <th>Age Category</th>
                        <th>Ticket Price</th>
                        <th>Reg. Link</th>
                    </tr>
                </thead>
                <tbody>';
    $id = get_the_ID();
    $current_date = date("Y-m-d H:i:s");
    if( get_field('Race-Categories', $id) ) {
        while( the_repeater_field('Race-Categories', $id) ) {
        $event_start_date .= date("Y-m-d H:i:s", strtotime("+5 hours +30 minutes", strtotime(get_sub_field("ticket_registration_start"))));
        $event_end_date .= date("Y-m-d H:i:s", strtotime("+5 hours +30 minutes", strtotime(get_sub_field("ticket_registration_end_date"))));
        $result .='<tr>';
        $result .='<td>'.get_sub_field("package").'</td>';
        $result .='<td>'.get_sub_field("age_category").'</td>';
        $result .='<td>'.get_sub_field("ticket_price").'</td>';
        if($current_date > $event_start_date){
            $result .= '<td>Registration Open</td>';
        }else{
            $result .= '<td>Registration Close</td>';
        }
        $result .='</tr>';
    }
    }else{
        $result .='No Any Race categories allocated for this event. Try other events.';
    }
    $result .= '</tbody>
                </table>
                </div>';
    return $result;
}

但这是返回错误的输出,在我的输出的屏幕截图下方分享。

”“在此处输入图像说明”

它在每个行注册中都显示,但从逻辑上讲,它应该关闭注册,并仅显示注册为2 km家族的注册-跑步。

I have some dates for my tickets for the event like the following screenshot and I am using ACF repeater for this.

enter image description here

And I have a function where I am trying to compare the ticket registration start date with the current date.

function santy_shortcode_race_categories_event_post(){
    date_default_timezone_set('Asia/Kolkata');
    $result = '<div class="table-responsive">
                <table class="table table-bordered" style="text-align:center; background-color:#fff;">
                <thead>
                    <tr class="table-head">
                        <th>Package</th>
                        <th>Age Category</th>
                        <th>Ticket Price</th>
                        <th>Reg. Link</th>
                    </tr>
                </thead>
                <tbody>';
    $id = get_the_ID();
    $current_date = date("Y-m-d H:i:s");
    if( get_field('Race-Categories', $id) ) {
        while( the_repeater_field('Race-Categories', $id) ) {
        $event_start_date .= date("Y-m-d H:i:s", strtotime("+5 hours +30 minutes", strtotime(get_sub_field("ticket_registration_start"))));
        $event_end_date .= date("Y-m-d H:i:s", strtotime("+5 hours +30 minutes", strtotime(get_sub_field("ticket_registration_end_date"))));
        $result .='<tr>';
        $result .='<td>'.get_sub_field("package").'</td>';
        $result .='<td>'.get_sub_field("age_category").'</td>';
        $result .='<td>'.get_sub_field("ticket_price").'</td>';
        if($current_date > $event_start_date){
            $result .= '<td>Registration Open</td>';
        }else{
            $result .= '<td>Registration Close</td>';
        }
        $result .='</tr>';
    }
    }else{
        $result .='No Any Race categories allocated for this event. Try other events.';
    }
    $result .= '</tbody>
                </table>
                </div>';
    return $result;
}

But this is returning wrong output, going share below the screenshot of my output..

enter image description here

It is showing in every row registration open but logically it should be show registration closed and show only registration open for 2 km Family-Run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2025-02-08 22:18:32

我认为您的问题是您在循环的每个实例中加入日期,应该分配:

尝试更改以下内容:

$event_start_date .= date("Y-m-d H:i:s"...
$event_end_date .= date("Y-m-d H:i:s"...

对此:

$event_start_date = date("Y-m-d H:i:s"...
$event_end_date = date("Y-m-d H:i:s"...

I think your issue is that you are concatenating the dates in each instance of the loop and should be assigning:

Try changing this:

$event_start_date .= date("Y-m-d H:i:s"...
$event_end_date .= date("Y-m-d H:i:s"...

To this:

$event_start_date = date("Y-m-d H:i:s"...
$event_end_date = date("Y-m-d H:i:s"...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文