【问题标题】:How to display particular content at particular times in php?如何在php中的特定时间显示特定内容?
【发布时间】:2019-03-01 14:48:46
【问题描述】:

我正在开发一个网站,我想在特定时间在 php 中显示特定内容。

从美国东部时间周一下午 6 点(东部标准时间)到周五下午 6 点(东部标准时间),我希望显示特定的内容。为此,我使用以下逻辑。

$arradate = strtolower(date('D'));
date_default_timezone_set('America/Toronto');
$nowtime = date('H:i:s');
$tue_thu = array('tue','wed','thu');

if (in_array($arradate, $tue_thu) || ($arradate == 'mon' && $nowtime > '17:59:59') || ($arradate == 'fri' && $nowtime < '17:59:59')) {
echo "Hello World";
}     

问题陈述:

我想知道代码中是否有任何问题,因为它在星期四不起作用。我在 EST,服务器设置为 UTC。

【问题讨论】:

  • 第一行获取设置时区之前的日期。这可能是个问题。
  • @Bryan 这是一个问题。我还需要做其他更改吗?
  • @Bryan 星期四没用。
  • @Bryan 我想知道我比较时间的方式是否正确?

标签: php time timezone


【解决方案1】:

您的逻辑过于复杂。 DateTime 对象具有可比性,因此您可以直接检查某个时刻是否在另一个时刻之后/之前:

$timezone = new DateTimeZone( 'America/Toronto' );
$start    = ( new DateTime( 'monday this week', $timezone ) )->setTime( 18, 0 );
$end      = ( new DateTime( 'friday this week', $timezone ) )->setTime( 18, 0 );
$now      = new DateTime( 'now', $timezone );

if ( $now > $start && $now < $end ) {
    echo "Hello World";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2013-01-08
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多