我需要你的帮助。在我的 php 代码中,我创建了一个日期倒计时,它可以正常工作,但是我在 jquery 中的代码有问题。我正在尝试获取 $json_result 值。这是我的 php 代码:
<?php
function addZero($addZero){
return ($addZero < 10) ? '0'.$addZero : $addZero;
}
header('Content-type: application/json');
$year_value = date('Y');
$end_month = $_GET['month'];
$end_day = $_GET['day'];
$end_hours = $_GET['hour'];
$date = strtotime("$year_value-$end_month-$end_day, $end_hours:00:00");
$remaining = $date - time();
$minutes = floor($remaining/60);
$hours = floor($remaining/3600);
$daysLeft = floor($remaining/86400);
if($daysLeft > 0){
$remaining_hours = $hours - ($daysLeft * 24);
}else{
$remaining_hours = $hours;
}
$remaining_minutes = floor(($remaining - ($hours * 3600))/60);
$remaining_seconds = floor($remaining - ($minutes * 60));
$result = 'Days left: '.$daysLeft.' '.addZero($remaining_hours).':'.addZero($remaining_minutes).':'.addZero($remaining_seconds);
$json_result = json_encode($result);
echo $json_result;
?>
这是我的 html jquery php 代码:
<head>
<script type="application/javascript">
jQuery(document).ready(function(){
$("#submit").click(countDown);
});
function countDown(e){
setTimeout(function(){
$.get('countdown.php',function(data){
$("#countDown").html(data);
e.preventDefault();
});
countDown();
},1000);
}
</script>
</head>
<body>
<?php
$monthsArray = array(0 => '0', 1 => 'Jan.', 2 => 'Feb.', 3 => 'Mar.', 4 => 'Apr.', 5 => 'May', 6 => 'Jun.', 7 => 'Jul.',
8 => 'Aug.', 9 => 'Sep.', 10 => 'Oct.', 11 => 'Nov.', 12 => 'Dec.');
$months = array_slice($monthsArray, date('m'), 12, true);
?>
<label>Month:</label>
<select name="month" id="month">
<?php
foreach ($months as $values => $keys) {
printf('<option value="%u">%s</option>', $values, $keys);
}
?>
</select>
<?php
$daysArray = array(0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9', 10 => '10',
11 => '11', 12 => '12', 13 => '13', 14 => '14', 15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19', 20 => '20', 21 => '21',
22 => '22', 23 => '23', 24 => '24', 25 => '25', 26 => '26', 27 => '27', 28 => '28', 29 => '29', 30 => '30', 31 => '31');
if(date('L') == 0 && (date('m') == 02 || date('m') == 2)){
$days = array_slice($daysArray, date('d'), -3, true); // if it's not a leap year february has 28 days
}elseif(date('L') == 1 && (date('m') == 02 || date('m') == 2)){
$days = array_slice($daysArray, date('d'), -2, true); // if it's a leap year february has 29 days
}elseif(date('m') % 2 == 0){
$days = array_slice($daysArray, date('d'), -1, true); // even month
}else{
$days = array_slice($daysArray, date('d'), 31, true); // odd month
}
?>
<label>Day:</label>
<select name="day" id="day">
<?php
foreach ($days as $values => $keys) {
printf('<option value="%u">%s</option>', $values, $keys);
}
?>
</select>
<input type="submit" id="submit" value="Submit">
<div id="countDown">
</div>
</body>
有人可以告诉我我做错了什么吗?我正在使用 jquery 1.9.1 版。