指定日期折扣的脚本

发布于 2024-12-29 15:15:54 字数 980 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎会甘心 2025-01-05 15:15:54
<?
date_default_timezone_set('MST');

$now = new DateTime();

// 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'    => new DateTime('2012-01-30 15:38:04'),
    'Seattle_seminar' => new DateTime('2012-07-23 00:00:00'),
    'Florida_seminar' => new DateTime('2012-10-05 00:00:00')
);

foreach ($array as $seminar => $date) {
    $cutoff = clone $date;
    $cutoff->modify('-5 days');

    if ($now > $cutoff) {
        echo "$seminar - discount is not eligible\n";
    } else {
        echo "$seminar - discount received\n";
    }
}
?>
<?
date_default_timezone_set('MST');

$now = new DateTime();

// 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'    => new DateTime('2012-01-30 15:38:04'),
    'Seattle_seminar' => new DateTime('2012-07-23 00:00:00'),
    'Florida_seminar' => new DateTime('2012-10-05 00:00:00')
);

foreach ($array as $seminar => $date) {
    $cutoff = clone $date;
    $cutoff->modify('-5 days');

    if ($now > $cutoff) {
        echo "$seminar - discount is not eligible\n";
    } else {
        echo "$seminar - discount received\n";
    }
}
?>
ぃ弥猫深巷。 2025-01-05 15:15:54

因为您使用的是 break,所以您的循环只会运行一次。如果我明白你想要做什么,你应该使用:

$eligable = false;
foreach ($array as $seminar_date) {
  if ($discount_check < $seminar_date) {
    $eligable = true;
    break;
  }
}

if ($eligable) {
  echo 'discount received';
} else {
  echo 'discount is not eligible';
}

我将 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:

$eligable = false;
foreach ($array as $seminar_date) {
  if ($discount_check < $seminar_date) {
    $eligable = true;
    break;
  }
}

if ($eligable) {
  echo 'discount received';
} else {
  echo 'discount is not eligible';
}

I changed the while loop to foreach as I think they're more trivial to understand

柒夜笙歌凉 2025-01-05 15:15:54

根据您想要对数组执行的操作,我建议您进行排序,然后确定第一个适用的索引。

$discount_check = mktime(date());
//Sort array in reverse order
arsort($array);
for($i = 0; $i < count($array);$i++){
    //If array value meets the discount criteria, 
    if($array[$i]  <=   $discount_check - 432000){
    //The index of the first array item to match the discount criteria, since array is sorted all subsquent are applicable
        $split = $i;
        $i = count($i);
    }   
}

Depending on what you're looking to do with the array I'd recommend sorting and then determining the first applicable index.

$discount_check = mktime(date());
//Sort array in reverse order
arsort($array);
for($i = 0; $i < count($array);$i++){
    //If array value meets the discount criteria, 
    if($array[$i]  <=   $discount_check - 432000){
    //The index of the first array item to match the discount criteria, since array is sorted all subsquent are applicable
        $split = $i;
        $i = count($i);
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文