php - 创建商店时钟功能 - 根据时间打开/关闭和事件
问题描述
所以我有一个运行正常的代码。我收到了一些帮助,因为尽管我对自己的编码技能(相当新)感到相当自信 - 时间避开了我,但我发现确定它在 PHP 中的工作方式令人困惑。
我正在寻找一个函数来充当我的商店时钟并返回“打开”-“关闭”-“1 小时关闭”结果,我可以在其他函数中使用。
例如:
if ( store_clock() == 'open' ) {
echo 'Open';
}
elseif ( store_clock() == '1_hr_left' ) {
echo 'You have 27 mins and 12 secs to place an order' );
else {
echo 'Sorry, we are closed.';
}
我目前有以下代码在关闭时间、正确显示剩余时间倒计时等方面正常工作。问题是我没有打开时间,我更愿意将代码拆分 - 允许我使用store_clock()
触发整个站点的其他事件的功能......不仅仅是关闭计数器。
为此,我假设我需要 2 到 3 个不同的功能,而不仅仅是 1 个……但是我感到非常困惑。任何帮助表示赞赏。(PS。get_field()
正在调用 ACF 值)
function sat_clock() {
$the_time = current_time('timestamp');
$exact_time = gmdate('g:ia', $the_time);
$weekday_close = get_field('weekday_close', 'options');
$sat_close = get_field('sat_close', 'options');
$sun_close = get_field('sun_close', 'options');
// Set your closing times here from sun-sat
$closing_times = array( $sun_close, $weekday_close, $weekday_close, $weekday_close, $weekday_close, '7:31 pm', $sat_close );
if ( $closing_times[date( 'w', current_time('timestamp') )] !== false ) {
$closing_time = strtotime( sprintf( '%s %s', date( 'd F Y', current_time('timestamp') ), $closing_times[date( 'w', current_time('timestamp') )] ) );
if ( $closing_time - current_time('timestamp') > 0 && $closing_time - current_time('timestamp') < 1800 ) {
echo '<strong>LAST CALL</strong><p class="m-0" 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 = "You have <strong>" + minutes + "</strong> minutes " + "<strong>" + seconds + "</strong> seconds 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 ( $closing_time - current_time('timestamp') < 0 ) {
echo "<p>Closed for today</p>";
} else {
echo "<p>Opened</p>" . gmdate('g:ia', $closing_time);
var_dump($exact_time);
}
} else {
echo "<p>Closed for today</p>";
}
}
add_action( 'woocommerce_archive_description', 'sat_clock' );