时间依赖条件

时间:2016-08-18 14:58:28

标签: php time

我正在尝试在我的网站上显示一条消息,该消息取决于一天中的时间和一周中的某天,由于需要在当天下午2点后显示当天24小时的名称,因此更加复杂。

到目前为止,我有这个很接近,但似乎不正确,加上它可怕的丑陋和简单。

有人可以指出为什么/哪里没有按预期运作?

<?php if (date("w") != 0 && date("w") != 6 && date("w") != 5):
    $future_date = strtotime('tomorrow 14:00 Europe/London');
    $remaining = $future_date-strtotime('now');
    $tomorrow = strtotime('today 14:00 +601 minutes');
    $c_time = mktime('H:i');
    $open = strtotime('yesterday 14:00');
    $close = strtotime('today 14:00'); ?>

<?php if ($c_time > $open && $c_time < $close): ?>
 <div class="margin-top-5">
 <p style="padding:10px 0; font-size: 12px;">Only <span class="timerHighlight">
  <?php echo date('H', $remaining);?> hours, <?php echo date('i', $remaining);?> minutes</span> left to get this product delivered on <span style="color:orange; font-weight: bold; text-transform: capitalize;">
    <?php echo date('l', $tomorrow);?></span> using <strong>'Next Working Day'!</strong></p>
    </div>
  <?php else: ?>
    <p><b>*Order before 2pm for next day delivery.</b></p>
  <?php endif; ?>
<?php endif; ?>

3 个答案:

答案 0 :(得分:0)

我喜欢这类DateTimeDateInterval类。 我已经尝试了你正在做的事情,所以你应该能够通过使用以下内容作为参考。

$now = new DateTime('now');
$orderWithin = null;
$deliveryDate = null;

switch ($now->format('w')) {

    // Order before 6pm Monday-Thursday and get it next day
    case 1: // Monday
    case 2: // Tuesday
    case 3: // Wednesday
    case 4: // Thursday
        $orderWithin = clone $now;
        $orderWithin->setTime(18, 00, 00);
        // Next day delivery
        $deliveryDate = clone $now;
        $deliveryDate->add(new DateInterval('P1D'));
        break;

    // Order before 6pm Friday and get it next working day (Monday)
    case 5: // Friday
        $orderWithin = clone $now;
        $orderWithin->setTime(18, 00, 00);
        // Next working day delivery
        $deliveryDate = clone $now;
        $deliveryDate->add(new DateInterval('P1D'));
        break;

    // Order on Sunday or Saturday then get it Tuesday
    case 0: // Sunday
        $orderWithin = clone $now;
        $orderWithin->setTime(23, 59, 59);
        // Tuesday delivery
        $deliveryDate = clone $now;
        $deliveryDate->add(new DateInterval('P1D'));
    case 6: // Saturday
        $orderWithin = clone $now;
        $orderWithin->add(new DateInterval('P1D'));
        $orderWithin->setTime(23, 59, 59);
        // Tuesday delivery
        $deliveryDate = clone $now;
        $deliveryDate->add(new DateInterval('P2D'));
        break;
}

$orderWithinInterval = $orderWithin->diff($now);

$orderWithinString = "{$orderWithinInterval->d} day(s), {$orderWithinInterval->h} hour(s), {$orderWithinInterval->i} minute(s)";

$deliveryDateString = "{$deliveryDate->format('D jS M')}";

?>

<p>Order within <?= $orderWithinString ?> to get your item delivered by <?= $deliveryDateString ?>.</p>

我的输出看起来像这样:

  

在0天(星期五)之前的0天内,2小时(29)分钟内完成订单。

答案 1 :(得分:0)

现在按预期工作的修订版本:

<?php if (!in_array(date("w"),array(0,6,5),true)):
                      $future_date = strtotime('tomorrow 14:00 Europe/London');
                      $remaining = $future_date-strtotime('now');
                      $tomorrow = strtotime('today 14:00 +601 minutes Europe/London');
                      $c_time = mktime();
                      $open = strtotime('yesterday 13:59 Europe/London');
                      $close = strtotime('today 13:59 Europe/London'); ?>

                      <?php if ($c_time > $open && $c_time < $close): ?>
                      <div class="margin-top-5">
                          <p style="padding:10px 0; font-size: 12px;">
                              Only <span class="timerHighlight">
                                                <?php echo date('H', $remaining);?> hours, <?php echo date('i', $remaining);?> minutes</span> left to get this product delivered on <span style="color:orange; font-weight: bold; text-transform: capitalize;">
                                                <?php echo date('l', $tomorrow);?></span> using <strong>'Next Working Day'!</strong></p>
                      </div>
                  <?php else: ?>

                      <p><b>*Order before 2pm for next day delivery.</b></p>
                  <?php endif; ?>


                      <br>
                  <?php endif; ?>

问题区域似乎是mktime(&#39; H:i&#39;);和strtotime上的时区()。下一步是清理并提高效率。

答案 2 :(得分:0)

我现在已经进一步采取了它并使它适用于3种不同的场景,它仍然臃肿和丑陋,但是因为这是我第一次正确地尝试制作一个真实的东西世界形势我很高兴它有效!

随意挑选它并指出最佳实践方法:

html{height:100%;} body{min-height:100%}
相关问题