我正在尝试根据一小时显示一些内容。 这小时来自DB作为字符串字段。 如果当前时间与DB匹配或传递时间,则应显示某个内容,否则为默认内容
到目前为止我尝试了什么:
$time = strtotime($p_objetct->getHour());
//we check if this has already passed to
//retrieve corresponding content else we display default content
if ($time >= date("h:i:sa")) {
echo 'time to display new content !';
}else{
echo 'display content default';
}
此代码效果不佳,因为您可以猜测,因为如果我想在20:00:00显示新内容,当前时间是08:00:00时,第1个条件为真。
我也试过了
$time = strtotime(p_objetct->getHour());
$selection = floor((date('G',$time) / 2));
switch ($selection) {
case 0:
echo 'time between 0-2!';
break;
case 1:
echo 'time between 2-4!';
break;
case 2:
echo 'time between 4-6!';
break;
case 3:
echo 'time between 6-8!';
break;
case 4:
echo 'time between 8-10!';
break;
case 5:
echo 'time between 10-12!';
break;
case 6:
echo 'time between 12-14!';
break;
case 7:
echo 'time between 14-16!';
break;
case 8:
echo 'time between 16-18!';
break;
case 9:
echo 'time between 18-20!';
break;
case 10:
echo 'time between 20-22!';
break;
case 11:
echo 'time between 22-24!';
break;
}
但这不是一个好方法,因为我无法猜测在哪种情况下来自DB的时间将是......
欢迎任何想法!
修改:
我有2个内容(新的和默认的)。 新内容应该从DB到00:00:00的时间显示,然后它应该是默认的
答案 0 :(得分:1)
如果您只使用@app.route('/temperatuur', methods=['GET','POST',])
def temperatuur():
zipcode = request.form['zip']
r = requests.get('http://api.openweathermap.org/data/2.5/weather?zip='+zipcode+',be&APPID=84c7d83bae2f2396ebd3a4a48dfdd057')
json_object = r.json()
weer = json_object['weather',[1]]
temp_k = int(json_object['main']['temp'])
temp_c = (temp_k - 273)
plaats = str(json_object['name'])
return render_template('temperatuur.html', temperatuur=temp_c, plaats = plaats, weer = weer)
方法获得24小时格式的小时,则可以将其与$p_objetct->getHour()
进行比较。
如果你看完完全匹配,你可以使用:
date('H');
如果您需要它作为下限时间随意使用并更改此代码段:
<?php
$hourToCheck = $p_objetct->getHour();
$currentHour = date('H'); // As of PHP manual "H" returns the hour in 24h format
$contentToDisplay = "What ever you wanna display as default.";
if ($hourToCheck === $currentHour) {
$contentToDisplay = "Your matching content.";
}
答案 1 :(得分:0)
您可以尝试使用基于时间的名称设置变量,使用该名称匹配现有文件并使用php进行匹配。像这样:
<?php
$time = date('H',time());
$name_of_content_to_require = false;
switch ($time) {
case 0:
$name_of_content_to_require = '0-2';
break;
case 1:
$name_of_content_to_require = '2-4';
break;
case 2:
$name_of_content_to_require = '4-6';
break;
case 3:
$name_of_content_to_require = '6-8';
break;
case 4:
$name_of_content_to_require = '8-10';
break;
case 5:
$name_of_content_to_require = '10-12';
break;
case 6:
$name_of_content_to_require = '12-14';
break;
case 7:
$name_of_content_to_require = '14-16';
break;
case 8:
$name_of_content_to_require = '16-18';
break;
case 9:
$name_of_content_to_require = '18-20';
break;
case 10:
$name_of_content_to_require = '20-22';
break;
case 11:
$name_of_content_to_require = '22-24';
break;
}
if( $name_of_content_to_require ) {
require_once __DIR__ . $name_of_content_to_require . '.php';
} else {
die('with some error, or throw an exception');
}