【问题标题】:Display content based on date and time根据日期和时间显示内容
【发布时间】:2014-08-13 05:37:47
【问题描述】:

我需要在特定日期和时间显示内容。

我目前正在使用一个脚本,它根据星期几显示内容,但需要扩展它以在两组小时之间显示内容,因此在星期一上午 12 点到下午 5 点之间然后在下午 5 点之间显示内容和晚上 11.59 点,一周中的每一天都一样。

<?php 
if( date( 'w' ) == 0 ) { 
?> 
<div>Write what you need to show here on Sunday</div> 
<? 
} 
if( date( 'w' ) == 1 ) { 
?> 
<div>Write what you need to show here on Monday</div>
<? 
} 
?> 
<? 
if( date( 'w' ) == 2 ) { 
?> 
<div>Write what you need to show here on Tuesday</div>
<? 
} 
?> 
<? 
if( date( 'w' ) == 3 ) { 
?> 
<div>Write what you need to show here on Wednesday</div> 
<? 
} 
?> 
<? 
if( date( 'w' ) == 4 ) { 
?> 
<div>Write what you need to show here on Thursday</div> 
<? 
} 
?> 
<? 
if( date( 'w' ) == 5 ) { 
?> 
<div>Write what you need to show here on Friday</div> 
<? 
} 
?> 
<? 
if( date( 'w' ) == 6 ) { 
?> 
<div>Write what you need to show here on Saturday</div> 
<? 
} 
?>

【问题讨论】:

  • 你的问题是什么?
  • 在每个条件中添加另一个条件以检查您想要的时间段。

标签: php date time


【解决方案1】:

放下上午和下午,在 24 小时内思考。

if (date('w') == 1) {
  if (date('H') >= 0 && date('H') < 17) {
    // the thing you want on monday 0:00 (12 am) to 17:00 (5pm)
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2020-07-17
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多