在收到@rx2347下面的帮助后,除了一件事之外,一切都在正常工作。我对我的问题进行了编辑,以便更好地解释这个问题。
我有一个json数组,我正在提取某些数据,如"id"one_answers"start"。start一起显示日期和时间。我正在将日期与日历日期进行比较,并尝试在正确的日期中插入正确的时间。有点像一个匹配的游戏。
目前一切正常,但是插入只是插入数组中的最后一个值,我需要它来插入每个值。在@rx2347 的帮助下,我现在陷入了困境
<?php
$url = 'JSON ARRAY';
$data = file_get_contents($url);
$array = json_decode($data, true);
foreach($array as $your_json_date) {
//convert date and time
$your_date = strftime("%G%m%d", strtotime($your_json_date["start"]));
$your_time = strftime("%I:%M %P",strtotime($your_json_date["start"]));
$your_id = $your_json_date["id"];
$result = '<a href="MY URL'.$your_id .'">'.$your_time.'</a>';
}
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}
?>
<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr>
<td width="50%" align="left"><a href="<?php echo "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" cellpadding="2" cellspacing="2" border="1">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<th><strong>S</strong></th>
<th><strong>M</strong></th>
<th><strong>T</strong></th>
<th><strong>W</strong></th>
<th><strong>T</strong></th>
<th><strong>F</strong></th>
<th><strong>S</strong></th>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
// compare your date and calendar date
$thisdate = strftime("%G%m%d",strtotime($cMonth."/".($i - $startday + 1)."/".$cYear));
if($your_date == $thisdate) $time = $result; else $time = "";
if(($i % 7) == 0 ) echo "<tr>";
if($i < $startday) echo "<td></td>";
else {
echo "<td align='center' valign='middle' height='20px'>";
echo ($i - $startday + 1);
//output time
echo "<br><b>".$time."</b>";
echo "</td>";
}
if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>
所以,您只需要知道如何比较这两个日期,对吧?
第一:使用strtotime和strftime 将两个日期转换为相同的格式
$date = "05/02/2020 1:00 PM";
echo strftime("%G%m%d",strtotime($date));
这将把您的JSON日期转换为"20200502",无论何时。对日历中的PHP日期执行同样的操作,并检查它们是否相等。完成。
我希望我能正确地理解你。
这是修改后的代码:
<?php
//your json date
$your_json_date = "05/02/2020 1:00 PM";
//convert date and time
$your_date = strftime("%G%m%d", strtotime($your_json_date));
$your_time = strftime("%I:%M %P",strtotime($your_json_date));
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}
?>
<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr>
<td width="50%" align="left"><a href="<?php echo "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" cellpadding="2" cellspacing="2" border="1">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<th><strong>S</strong></th>
<th><strong>M</strong></th>
<th><strong>T</strong></th>
<th><strong>W</strong></th>
<th><strong>T</strong></th>
<th><strong>F</strong></th>
<th><strong>S</strong></th>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
// compare your date and calendar date
$thisdate = strftime("%G%m%d",strtotime($cMonth."/".($i - $startday + 1)."/".$cYear));
if($your_date == $thisdate) $time = $your_time; else $time = "";
if(($i % 7) == 0 ) echo "<tr>";
if($i < $startday) echo "<td></td>";
else {
echo "<td align='center' valign='middle' height='20px'>";
echo ($i - $startday + 1);
//output time
echo "<br><b>".$time."</b>";
echo "</td>";
}
if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>
查看链接的PHP文档以了解进一步的更改。