我需要一个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';
似乎不起作用(是的,我只是猜测它应该如何工作)。我很感激一些反馈是否我的脚本是正确的,我应该如何测试它?谢谢!
This
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";
日期返回一个根据给定格式字符串格式化的字符串,使用给定的整数时间戳,如果没有给出时间戳,则返回当前时间。
其中为时间,
返回当前Unix时间戳
和strtotime一起使用
将任何英文文本日期时间描述解析为Unix时间戳
将当前时间与开始时间和结束时间进行比较。