将当前时间与数组中的值true / false进行比较

时间:2015-08-17 10:44:05

标签: php arrays time

我想为一周7天定义一组开放时间。当访问者访问该网页时,我想显示特定国家/地区(尼维斯,加勒比地区)的当前时间和日期,然后将其与一组定义的开放时间进行比较,以显示两个字幕中的一个1)打开2)关闭。具体来说,这就是我想要制作的内容:

enter image description here

我到目前为止使用这个来获取当前时间并设置数组但是如何比较这两个?

<?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"
   );

   ?>

<?php echo get_timee("Federation of Saint Kitts and Nevis","Nevis"); ?>。我们现在......

2 个答案:

答案 0 :(得分:1)

概述解决方案:

  1. 计算开盘时间数组的数字版本。此数组将是星期几到开放时间的映射,将DateTime-&gt;格式('w')的日期编入索引,从星期日开始为0,等等。每个条目可以为空,无开放时间,或包含商店打开日期的秒数的数组。

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

  3. 确定计算的DateTime对象是否在当前日期的范围内。特别是,使用(int)$ dateTime-&gt;格式('w')来获取索引,然后在数值数组中查找。如果条目不为null,则对dateTime项执行“秒”计算,并确定它是否在该范围内。

答案 1 :(得分:1)

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

但是你可以使用像这样的核心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