比较从API到今天的日期

时间:2016-02-05 17:30:13

标签: javascript php jquery date

我目前正在展示"灯具"一个网站上的体育团队,但从其他网站抓取所有数据。

我有一个API可以获取所有数据,包括下一个对手,日期,时间等等。

我将每个灯具发布到一个列表中,然后将每个元素发布到不同的div中。

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将给定的日期和今天的日期结构化为可比较的格式,然后将它们相互比较并显示最新的日期 - 但我似乎无法用不止一个列表项。

我想添加一个&#34;隐藏&#34;在今天日期之前对每个列表项进行分类。

这是我到目前为止所得到的:

<?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>

我不认为我离答案很远,但我现在只是绊倒了自己!

提前致谢。

1 个答案:

答案 0 :(得分:0)

多个列表项应该发生的事情应该在循环中。像这样:

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

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

相关问题