指定日期折扣的脚本
我对 PHP 还不太熟悉,所以我的脚本编写仍在进行中。我有一个页面,用户可以注册参加他们必须付费的研讨会。如果他们在研讨会举行前 5 天之前注册,他们将获得折扣价;如果他们在研讨会举行前 5 天之后注册,他们必须支付全价。我已经编写了一个 PHP 脚本,我认为它可以做到这一点(无论如何,它正在测试中),但我感觉有更好的方法来编写它。我只是在寻找任何人的建议(如果有的话)。非常感谢您的帮助。我的脚本如下。
date_default_timezone_set('MST');
$discount_check = date("YmdHis", mktime(date("H"), date("i"), date("s"), date("m") , date("d")+5 , date("Y") ));
//Made an array because there are several seminars a year and because I need to make it easy enough for others in my office (who don't know PHP) to add/edit seminar dates
$array = array(
'Utah_seminar' => '20120130153804',
'Seattle_seminar' => '20120723000000',
'Florida_seminar' => '20121005000000'
);
while ($seminar_dates = current($array))
{
if ($discount_check >= $seminar_dates)
{
echo 'discount is not eligible';
break;
}
else
{
echo 'discount received';
break;
}
next($array);
}
再次强调,我们非常感谢任何帮助。即使发现代码有任何问题。
I'm a little new at PHP, so my script writing is still progressing. I have a page that users register for a seminar they have to pay for. If they register prior to 5 days before the seminar takes place, they get a discounted price, if they register post 5 days before the seminar takes place, they have to pay full price. I have written a PHP script that I think will do that (works is testing anyway) but I have a feeling there is a better way of writing it. I am just looking for any suggestions anyone has, if any. Your help is greatly appreciated. My script is as follows.
date_default_timezone_set('MST');
$discount_check = date("YmdHis", mktime(date("H"), date("i"), date("s"), date("m") , date("d")+5 , date("Y") ));
//Made an array because there are several seminars a year and because I need to make it easy enough for others in my office (who don't know PHP) to add/edit seminar dates
$array = array(
'Utah_seminar' => '20120130153804',
'Seattle_seminar' => '20120723000000',
'Florida_seminar' => '20121005000000'
);
while ($seminar_dates = current($array))
{
if ($discount_check >= $seminar_dates)
{
echo 'discount is not eligible';
break;
}
else
{
echo 'discount received';
break;
}
next($array);
}
Again, any help is greatly appreciated. Even if it's finding any hiccups with the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您使用的是
break
,所以您的循环只会运行一次。如果我明白你想要做什么,你应该使用:我将
while
循环更改为foreach
因为我认为它们更容易理解Because you're using
break
your loop will only run once. If I understand what you're trying to do, you should use:I changed the
while
loop toforeach
as I think they're more trivial to understand根据您想要对数组执行的操作,我建议您进行排序,然后确定第一个适用的索引。
Depending on what you're looking to do with the array I'd recommend sorting and then determining the first applicable index.