2016-02-05 118 views
0

我目前正在一个网站上显示一个运动队的“固定装置”,但所有数据都是从另一个网站获取的。比较从API获取的日期到今天的日期

我有一个得到所有的数据,包括下一个对手,日期,时间等

我张贴每个灯具到列表中的踢,然后每个元素到不同的div的API。

E.g.每个灯具 - <li><div>Date of Fixture</div> <div>Kick off time</div> <div>Home Team</div> <div>Away Team</div></li>

我有div中的每个日期:每个列表项上的第一个,我有一个跨度中的当前日期。

我试过使用Date.js将给定的日期和今天的日期构造成可比较的格式,然后将它们相互比较并显示最新的一个 - 但我似乎无法用更多比一个列表项目。

我想在今天的日期之前为每个列表项添加一个“隐藏”类。

这里是我到目前为止有:

<?php 
$request = "https://www.kimonolabs.com/api/csv/9shgt89o?apikey=-----"; 
$response = file_get_contents($request); 
$results = json_decode($response); 
$currentdate = date("D d M y"); 
echo "<ul>\n\n"; 
$f = fopen("https://www.kimonolabs.com/api/csv/9shgt89o?apikey=-----", "r"); 
while (($line = fgetcsv($f)) !== false) { 
    echo "<li>"; 
    foreach ($line as $cell) { 
     echo "<div>" . htmlspecialchars($cell) . "</div>"; 
    } 
    echo "</li>\n"; 
    } 
fclose($f); 
echo "\n</ul>"; 
echo "<span>" . $currentdate . "</span>" 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> 
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> 
<script src="date.js"></script> 
<style> 
body { 
    font-family: 'Open Sans', sans-serif; 
} 

.hidden { 
    display:none; 
} 

ul li div { 
    display:inline-block; 
    padding:6px; 
} 

ul { 
    list-style:none; 
} 

ul li { 
    padding:6px; 
} 
</style> 
<head> 
<script> 
$(document).ready(function(){ 
    $('ul li:nth-child(1)').addClass('hidden'); 
    $('ul li:nth-child(2)').addClass('hidden'); 
    $('ul li div:nth-child(7)').addClass('hidden'); 
    $('ul li div:nth-child(8)').addClass('hidden'); 
    $('span').addClass('date'); 
}); 
var todaytext = $('span').text(); 
var today = Date.parse(todaytext); 
document.write('<p>' + today +'</p>'); 

$('ul li').each(function() { 
    Date.parse(this); 
}); 
var fixturetext = $('ul li:eq(div:first').text(); 
var fixture = Date.parse(fixturetext); 
document.write('<p>' + fixture +'</p>'); 


</script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
</body> 
</html> 

我不认为我是遥远的答案,但我只是绊倒自己的那一刻!

在此先感谢。

+0

在您的声明之前,您有* echo *声明。文档类型应该是任何HTML文档中的第一件事。 – David

+0

究竟是什么问题? – David

+0

嗨大卫 - 这不是一个问题,现在,这只是一个测试文件,以确保功能的工作 - 这不会活跃。 –

回答

0

应该发生在多个列表项上的事情应该在循环中进行。类似这样的:

$('ul li').each(function() { 
    var d = Date.parse(this); 
    if(d < today) 
     this.hide(); 
}); 

PHP语句应该放在HTML正文中。

+0

我正在解析列表中div的日期,但显然strotime工作正常,所以这是不必要的!不过,感谢您的帮助。 –

相关问题