如何在10月的第一个星期六测试PHP脚本?

时间:2015-10-04 19:03:49

标签: php date

我需要一个PHP脚本来显示从5月的第一个星期六到10月的第一个星期六的营业时间,然后是今年剩余时间的另一个字符串。这就是我蹒跚而行的事情:

$this_day = date('d-m-Y', strtotime("Today"));
$first_may = date('d-m-Y', strtotime("First Saturday Of May"));
$first_oct = date('d-m-Y', strtotime("First Saturday Of October"));

$sathours = 'Sat 10am-2pm';
if($this_day >= $first_may && $this_day <= $first_oct) { 
    $sathours = 'Sat by appt only';
} 
echo "$sathours";

创建后,我意识到我尝试通过重新定义当前日期来测试它

$this_day = date('d-m-Y', 02-10-2015);

$this_day = date('02-10-2015');

$this_day = '02-10-2015';

似乎不起作用(是的,我只是猜测它应该如何工作)。我很欣赏一些关于我的脚本是否正确以及我应该如何测试它的反馈?谢谢!

1 个答案:

答案 0 :(得分:1)

if($this_day >= $first_may && $this_day <= $first_oct) { 

正在比较字符串,而不是整数。因为$this_day和其他人都是字符串。我将值保持为整数。试一试..

date_default_timezone_set('America/New_York');
$this_day = time();
$first_may = strtotime("First Saturday Of May");
$first_oct = strtotime("First Saturday Of October");
$sathours = 'Sat 10am-2pm';
if($this_day >= $first_may && $this_day <= $first_oct) { 
    $sathours = 'Sat by appt only';
} 
echo "$sathours";

Date

  

使用给定的整数时间戳返回根据给定格式字符串格式化的字符串,如果没有给出时间戳,则返回当前时间。

time

的位置
  

返回当前的Unix时间戳

如果您将其与strtotime

一起使用
  

将任何英文文本日期时间描述解析为Unix时间戳

您将当前时间与两个开始和结束时间进行比较。