I need a PHP script to display business hours from 1st Saturday in May thru the 1st Saturday in October, then another string for the remainder of the year. Here is what I hobbled together:
我需要一个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";
After creating that, I realized my attempt to test it by redefining the current date
在创建之后,我意识到我试图通过重新定义当前日期来测试它
$this_day = date('d-m-Y', 02-10-2015);
or
要么
$this_day = date('02-10-2015');
or
要么
$this_day = '02-10-2015';
does not seem to work (yes, I'm just guessing at how that should work). I'd appreciate some feedback on whether my script is correct and how I should be testing it? Thanks!
似乎不起作用(是的,我只是猜测它应该如何工作)。我很欣赏一些关于我的脚本是否正确以及我应该如何测试它的反馈?谢谢!
1 个解决方案
#1
1
This
这个
if($this_day >= $first_may && $this_day <= $first_oct) {
is comparing strings, not integers. Because $this_day
, and others are strings. I'd keep the values as integers. Give this a try..
是比较字符串,而不是整数。因为$ 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";
日期
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
使用给定的整数时间戳返回根据给定格式字符串格式化的字符串,如果没有给出时间戳,则返回当前时间。
Where as time,
在哪里时间,
Return current Unix timestamp
返回当前的Unix时间戳
and if you use that with strtotime
如果你使用strtotime
Parse about any English textual datetime description into a Unix timestamp
将任何英文文本日期时间描述解析为Unix时间戳
You compare the current time to your two start and end times.
您将当前时间与两个开始和结束时间进行比较。
#1
1
This
这个
if($this_day >= $first_may && $this_day <= $first_oct) {
is comparing strings, not integers. Because $this_day
, and others are strings. I'd keep the values as integers. Give this a try..
是比较字符串,而不是整数。因为$ 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";
日期
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
使用给定的整数时间戳返回根据给定格式字符串格式化的字符串,如果没有给出时间戳,则返回当前时间。
Where as time,
在哪里时间,
Return current Unix timestamp
返回当前的Unix时间戳
and if you use that with strtotime
如果你使用strtotime
Parse about any English textual datetime description into a Unix timestamp
将任何英文文本日期时间描述解析为Unix时间戳
You compare the current time to your two start and end times.
您将当前时间与两个开始和结束时间进行比较。