php - 商店时钟 - 根据商店营业时间显示 30 分钟倒计时

标签 php wordpress datetime woocommerce

我想在我的电子商务商店关门前 30 分钟显示倒计时。基本上,您有“29 分 32 秒的时间来下订单”。

我有当前脚本,它从几个 ACF(高级自定义字段)时间选择器字段中提取商店营业时间。我还正确提取了我的商店日期和时间...我只是不知道如何将它们放在一起以在 30 分钟前显示消息并初始化倒计时。

function sat_clock() {
    $format = get_option('date_format') . ' ' . get_option('time_format');
    $hour = the_field('weekday_open', 'options');
    $times = array(
        'mon' => '9:00 AM - 9:00 PM',
        'tue' => '9:00 AM - 9:00 PM',
        'wed' => '9:00 AM - 9:00 PM',
        'thu' => '9:00 AM - 9:00 PM',
        'fri' => '9:00 AM - 9:00 PM',
        'sat' => '11:00 AM - 6:00 PM',
        'sun' => 'closed'
    );

    print date_i18n($format, current_time('timestamp'));
    print $hour;

}
add_action( 'woocommerce_archive_description', 'sat_clock' );

最佳答案

简单查看当前时间和当天收盘时间的差值。如果这少于 30 分钟(1800 秒)显示一点 JavaScript countdown timer从当前时间和当天收盘时间的差开始。

add_action( 'woocommerce_archive_description', 'sat_clock' );
function sat_clock() {

    $store_times = array(
        false, //Sunday 
        array( //Monday
            'open' => '09:00 AM',
            'close' => '09:00 PM',
        ), 
        array( //Tuesday
            'open' => '09:00 AM',
            'close' => '09:00 PM',
        ), 
        array( //Wednesday
            'open' => '09:00 AM',
            'close' => '09:00 PM',
        ), 
        array( //Thursday
            'open' => '09:00 AM',
            'close' => '09:00 PM',
        ), 
        array( //Friday
            'open' => '09:00 AM',
            'close' => '09:00 PM',
        ),
        array( //Saturday
            'open' => '11:00 AM',
            'close' => '06:00 PM',
        ), 
    );

    $current_day = date( 'w', current_time('timestamp') ); //Numeric representation of the day (0 for Sunday, 6 for Saturday)
    $current_date = date( 'd F Y', current_time('timestamp') );

    if ( $store_times[$current_day] !== false ) {

        $opening_time = strtotime( sprintf( '%s %s', $current_date, $store_times[$current_day]['open'] ) );
        $closing_time = strtotime( sprintf( '%s %s', $current_date, $store_times[$current_day]['close'] ) );

        if ( $closing_time > current_time('timestamp') && $closing_time - current_time('timestamp') < 1800 ) {
            echo "<p id='closing-soon-timer'></p>";
            ?>

            <script>
            var timeLeft = <?php echo $closing_time - current_time('timestamp'); ?>;
            
            // Update the count down every 1 second
            var x = setInterval(function() {

                // Time calculations for minutes and seconds
                var minutes = Math.floor(timeLeft / 60);
                var seconds = Math.floor(timeLeft % 60);

                // Display the result in the element with id="demo"
                document.getElementById("closing-soon-timer").innerHTML = minutes + "m " + seconds + "s to place an order.";

                // If the count down is finished, write some text
                if (timeLeft < 0) {
                    clearInterval(x);
                    document.getElementById("closing-soon-timer").innerHTML = "Closed for today";
                }

                timeLeft--;
            }, 1000);
            </script>

            <?php
        } elseif  ( $opening_time > current_time('timestamp') || $closing_time < current_time('timestamp') ) {
            echo "<p>Closed for today</p>";
        } else {
            echo "<p>Open for business</p>";
        }
    } else {
        echo "<p>Closed for today</p>";
    }
}

关于php - 商店时钟 - 根据商店营业时间显示 30 分钟倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64143759/

相关文章:

html - 通过CSS隐藏一些文本

sql - 从 SQL Server 2012 查询结果中减去小时数

javascript - 从 FullCalendar 获取日期并将其发送到 PHP

php - wordpress 页脚不在 footer.php 中

php - 如何将 WordPress 帖子水平显示为 3 列?

php - 基于 URL ID 加载图像

php - 如何在查询中使用之前转义字符串

mysql - 如何使用GROUP_CONCAT抓取行ID的数据?

c++ - 将 QString 日期(RFC 822 格式)转换为另一种基于文化的 QString 格式

php - 与 'reading initial communication packet' 处的 MySQL 服务器失去连接,系统错误 : 110