【问题标题】:Comparing dates grabbed from an API to todays date比较从 API 获取的日期与今天的日期
【发布时间】:2016-02-05 17:30:12
【问题描述】:

我目前正在一个网站上展示一支运动队的“赛程”,但所有数据都是从另一个网站获取的。

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

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

例如每个夹具 - <li><div>Date of Fixture</div> <div>Kick off time</div> <div>Home Team</div> <div>Away Team</div></li>

我在每个列表项的 div:first 中都有每个日期,并且我在跨度中有当前日期。

我尝试使用 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>

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

提前致谢。

【问题讨论】:

  • 在 .> 声明之前有 echo 语句。 doctype 应该是任何 HTML 文档中的第一件事。
  • 到底是什么问题?
  • 嗨,David - 现在这不是问题,这只是一个测试文档,以确保功能正常工作 - 这不会激活。

标签: javascript php jquery date


【解决方案1】:

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

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

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

【讨论】:

  • 我正在解析列表 div 中的日期,但显然 strotime 工作正常,所以这是不必要的!不过,感谢您的帮助。
猜你喜欢
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 2014-09-09
  • 2021-08-19
  • 1970-01-01
相关资源
最近更新 更多