将当前时间与数组中的值进行比较 true/false
Compare current time with values in an array true/false
我想定义一组每周 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×tamp=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"); ?>
。我们目前...
关于解决方案的概述:
计算您的开放时间数组的数字版本。该数组将是星期几到营业时间的映射,在 DateTime->Format('w') 的同一时间索引天数,从 0 开始表示星期日等。每个条目都可以为 null 表示没有营业小时数,或包含商店开张当天的秒数的数组。
将 get_timee 更改为 return DateTime 对象本身。
确定计算的 DateTime 对象是否在当前日期的范围内。特别是,使用 (int)$dateTime->format('w') 获取索引,然后在数值数组中查找。如果不为null,则对dateTime项进行"seconds"计算,判断是否在范围内。
您无需使用 google 任何东西即可完成所有这些操作。我假设您这样做是为了在客户所在地算出时间。也可能是因为您不确定服务器上的时区设置是什么,或者您知道服务器设置的时区不适合您所在的位置。
但是您可以像这样使用核心 PHP DateTime()
和 DateTimeZone()
类 来完成所有这些操作。
注意:我更改了 Opening/Closing 次数组以使其更易于处理!
<?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;
}
现在到 run/test 你要做的就是:
// 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
我想定义一组每周 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×tamp=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"); ?>
。我们目前...
关于解决方案的概述:
计算您的开放时间数组的数字版本。该数组将是星期几到营业时间的映射,在 DateTime->Format('w') 的同一时间索引天数,从 0 开始表示星期日等。每个条目都可以为 null 表示没有营业小时数,或包含商店开张当天的秒数的数组。
将 get_timee 更改为 return DateTime 对象本身。
确定计算的 DateTime 对象是否在当前日期的范围内。特别是,使用 (int)$dateTime->format('w') 获取索引,然后在数值数组中查找。如果不为null,则对dateTime项进行"seconds"计算,判断是否在范围内。
您无需使用 google 任何东西即可完成所有这些操作。我假设您这样做是为了在客户所在地算出时间。也可能是因为您不确定服务器上的时区设置是什么,或者您知道服务器设置的时区不适合您所在的位置。
但是您可以像这样使用核心 PHP DateTime()
和 DateTimeZone()
类 来完成所有这些操作。
注意:我更改了 Opening/Closing 次数组以使其更易于处理!
<?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;
}
现在到 run/test 你要做的就是:
// 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