【问题标题】:Compare current time with values in an array true/false将当前时间与数组中的值进行比较 true/false
【发布时间】:2015-08-17 10:44:05
【问题描述】:

我想定义一组每周 7 天的营业时间。当访问者访问网页时,我想显示特定国家(尼维斯、加勒比海)的当前时间和日期,然后将其与一组定义的打开时间进行比较,以显示两个标题之一 1)打开 2)关闭.具体来说,这是我想要制作的:

到目前为止,我一直在使用它来获取当前时间并设置数组,但是如何比较两者?

<?php
    function get_timee($country,$city) {
      $country = str_replace(' ', '', $country);
      $city = str_replace(' ', '', $city);
      $geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$city+$country,&sensor=false");
      $output_deals = json_decode($geocode_stats);
      $latLng = $output_deals->results[0]->geometry->location;
      $lat = $latLng->lat;
      $lng = $latLng->lng;
      $google_time = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$lng&timestamp=1331161200&key=xxx");
      $timez = json_decode($google_time);
      $d = new DateTime("now", new DateTimeZone($timez->timeZoneId));
      return  $d->format('H:i');
   }

   $array = array(
      "Monday" => "10:00 - 18:00",
      "Tuesday" => "10:00 - 18:00",
      "Wednesday" => "10:00 - 18:00",
      "Thursday" => "10:00 - 18:00",
      "Friday" => "18:00 - 23:00",
      "Saturday" => "18:00 - 23:00",
      "Sunday" => "Closed"
   );

   ?>

它的&lt;?php echo get_timee("Federation of Saint Kitts and Nevis","Nevis"); ?&gt;。我们目前...

【问题讨论】:

  • 您不应公开分享您的 API 密钥。
  • 我删除了它,比尼维斯晚好一点。我的意思是永远不会。

标签: php arrays time


【解决方案1】:

您无需使用任何 google 即可完成所有这些操作。我假设您这样做是为了在客户位置计算时间。或者可能是因为您不确定服务器上的时区设置为什么,或者您知道它设置为您所在位置的错误时区。

但是您可以使用像这样的核心 PHP DateTime()DateTimeZone() 类来完成所有这些工作。

注意:我更改了打开/关闭时间数组以使其更易于处理!

<?php

   $opening_times = array(
      "Monday" =>    array('open' => '10:00', 'close' => '18:00'),
      "Tuesday" =>   array('open' => '10:00', 'close' => '18:00'),
      "Wednesday" => array('open' => '10:00', 'close' => '18:00'),
      "Thursday" =>  array('open' => '10:00', 'close' => '18:00'),
      "Friday" =>    array('open' => '18:00', 'close' => '23:00'),
      "Saturday" =>  array('open' => '18:00', 'close' => '23:00'),
      "Sunday" =>   "Closed"
   );


function AreWeOpen($were_open, $date)
{
    $htm = '';
    if ( ! is_array($were_open[$date->format('l')]) 
        && strtolower($were_open[$date->format('l')]) == 'closed' ) 
    {
        $htm = 'We are closed all day Sunday';
    } else {
        if (   $date->format('H:i') >= $were_open[$date->format('l')]['open']
            && $date->format('H:i') <= $were_open[$date->format('l')]['close']
           )
        {
            $htm = 'We are open';
        } else {
            $htm = 'We are closed';
        }
    }
    return $htm;
}

现在要运行/测试上述所有内容:

    // set date time to NOW
    $date = new DateTime(null, new DateTimeZone('America/St_Kitts'));
    echo 'Are we open now? Now is ' . $date->format('l H:i') . ' >';
    echo AreWeOpen($opening_times, $date);

    echo PHP_EOL;

    echo 'Are we open at 09:59 Monday   > ';
    $date = new DateTime('2015/08/17 09:59:00', new DateTimeZone('America/St_Kitts'));
    echo AreWeOpen($opening_times, $date);

    echo PHP_EOL;

    echo 'Are we open at 10:00 Monday   > ';
    $date = new DateTime('2015/08/17 10:00:00', new DateTimeZone('America/St_Kitts'));
    echo AreWeOpen($opening_times, $date);

    echo PHP_EOL;

    echo 'Are we open at 18:00 Monday   > ';
    $date = new DateTime('2015/08/18 18:00:00', new DateTimeZone('America/St_Kitts'));
    echo AreWeOpen($opening_times, $date);

    echo PHP_EOL;

    echo 'Are we open at 18:01 Monday   > ';
    $date = new DateTime('2015/08/18 18:01:00', new DateTimeZone('America/St_Kitts'));
    echo AreWeOpen($opening_times, $date);

    echo PHP_EOL;

    echo 'Are we open at 18:01 Friday  > ';
    $date = new DateTime('2015/08/21 18:01:00', new DateTimeZone('America/St_Kitts'));
    echo AreWeOpen($opening_times, $date);


    echo PHP_EOL;

    echo 'Are we open on SUNDAY   > ';
    $date = new DateTime('2015/08/16 18:01:00', new DateTimeZone('America/St_Kitts'));
    echo AreWeOpen($opening_times, $date);

这给出了这些结果:

Are we open now? Now is Monday 07:38 >We are closed
Are we open at 09:59 Monday   > We are closed
Are we open at 10:00 Monday   > We are open
Are we open at 18:00 Monday   > We are open
Are we open at 18:01 Monday   > We are closed
Are we open at 18:01 Friday  > We are open
Are we open on SUNDAY   > We are closed all day Sunday

【讨论】:

  • 这太棒了。感谢您的解释。是的,我使用 Google 来获取尼维斯时间,但您的解决方案否定了所有这些,这甚至更好。
【解决方案2】:

关于解决方案的概述:

  1. 计算开放时间数组的数字版本。该数组将是星期几到营业时间的映射,索引日期与 DateTime->Format('w') 相同的时间,从 0 开始表示星期日等。每个条目可以为空,表示没有营业时间,或一个数组,其中包含商店开业当天的秒数。

  2. 更改 get_timee 以返回 DateTime 对象本身。

  3. 确定计算的 DateTime 对象是否在当前日期的范围内。特别是,使用 (int)$dateTime->format('w') 来获取索引,然后在你的数值数组中查找它。如果该项不为空,则对dateTime项进行“秒”计算,判断是否在范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多