我通过邮件将预订信息从php发送到javascript。使用floor($eventTime/(60*60*24)) - floor(time()/(60*60*24));
我告诉我的javascript代码根据他们的日期将事件放在哪里。 $eventTime
是一个unix时间戳。 E.x. 21日的任何日期都应该进入第一栏,22日的任何一天应该进入第2栏,等等。我做错了什么?以小时为单位的时间影响显示预订的日期,19日和20日的事件是混合的等等,但是我在数天内使用楼层,所以我不明白。要清楚,我想要从现在到另一个日期的天数,但不包括或考虑小时/分钟。因此,如果今天是第10天,无论什么时间(00:00:00 - 23:59:59)10日的任何预订应该将它的“timeFrom”变量表示为0.发生的事情是10日和11日的日期在这个例子中,当第10个应该是0而第11个应该是1时,将0作为“days between”变量发送。
这是相关的php
while ($appResult = $stmt->fetch(PDO::FETCH_ASSOC))
{
$tz = new DateTimeZone('America/Chicago'); // maybe this comes from the client side
$now = new DateTime('now', $tz);
$eventTime = new DateTime($appResult["time"], $tz);
$now->setTime(0, 0);
$eventTime->setTime(0, 0);
$diff = $now->diff($eventTime);
$days = $diff->d;
$postString .= $days;
$postString .= ".";
$postString .= $appResult['id'];
$postString .= ".";
$postString .= date("H.i",strtotime($appResult['time']));
$postString .= ".";
$postString .= $appResult['members'];
$postString .= ".";
$postString .= $appResult['admin'];
$postString .= ".";
$postString .= $appResult['message'];
$postString .= ".";
$postString .= $appResult['tools'];
$postString .= ".";
$postString .= $appResult['location'];
$postString .= ".";
$postString .= $appResult['time'];
$postString .= ".";
$postString .= $appResult['userCount'];
$postString .= ".";
$postString .= $appResult['class'];
$postString .= "!";
}
和相关的jquery
function(data)
{
$(".eventContainers > div:not(.eventHeaders) ").children().remove();
var dateDays = data.split("@")[0].split(",");
var appointments = data.split("@")[1].split("!")
var resultsContainer = $(".eventHeaders");
for(var i = 0; i < 5; i++)
{
$(resultsContainer[i]).find(".eventDay").text(dateDays[i].split(".")[0]);
$(resultsContainer[i]).find(".eventWeek").text(dateDays[i].split(".")[1]);
}
for(var i = 0; i < $(appointments).size() - 1; i++)
{
var inEvent = false;
for(var j = 0; j < $(appointments[i].split(".")[4].split(",")).size(); j++)
{
if(appointments[i].split(".")[4].split(",")[j] == id)
inEvent = true;
}
var appendString = "";
var parentStyle;
var buttonStyle;
var clicked;
var style = ['style="background-image:url(\'appAdd.png\')"','style="background-color:rgba(0,0,0,0.15)"','style="background-image:\\\'\\\'"','style="background-color:rgba(0,0,0,0.3)"'];
if(inEvent)
{
clicked = "true";
parentStyle = style[1];
buttonStyle = style[0];
}
else
{
clicked = "false";
parentStyle = style[3];
buttonStyle = style[2];
}
appendString += "<div id='" + appointments[i].split(".")[1] + "' class='appointmentIndivContainer' " + parentStyle + "><div class='appointmentJoinButton' " + buttonStyle + "></div><p class='appointmentTime'>" + getTime(appointments[i].split(".")[2], appointments[i].split(".")[3]) + "</p><p class='appointmentLocation'>" + appointments[i].split(".")[8].replace(/\+/g, ' ') + "</p> <p class='dataContainers admin'>" + appointments[i].split(".")[5] + "</p> <p class='dataContainers message'>" + appointments[i].split(".")[6].replace(/\+/g, ' ') + "</p> <p class='dataContainers tools'>" + appointments[i].split(".")[7].replace(/\+/g, ' ') + "</p> <p class='dataContainers userCount'>" + appointments[i].split(".")[9] + "</p><p class='dataContainers class'>" + appointments[i].split(".")[10] + "</p><p class='dataContainers clicked'>" + clicked + "</p><p class='dataContainers members'>" + appointments[i].split(".")[4].replace(/,/g, ', ') + "</p><p class='dataContainers daysFrom'>" + appointments[i].split(".")[0] + "</p></div>";
switch (appointments[i].split(".")[0])
{
case "0":
$("#dayOneEvents").append(appendString);
break;
case "1":
$("#dayTwoEvents").append(appendString);
break;
case "2":
$("#dayThreeEvents").append(appendString);
break;
case "3":
$("#dayFourEvents").append(appendString);
break;
case "4":
$("#dayFiveEvents").append(appendString);
break;
}
}
});
}
答案 0 :(得分:1)
获取时间差异的最简单方法是使用DateTime::diff
...
$tz = new DateTimeZone('Your/Timezone'); // maybe this comes from the client side
$now = new DateTime('now', $tz);
// get event time as a DateTime object.
// Use DateTime::createFromFormat if the input format is non-US or ambiguous
$eventTime = new DateTime('date/time string from client', $tz);
$diff = $now->diff($eventTime);
$days = $diff->d;
当然,这只会获得两个日期/时间之间的实际完整天数。 DateInterval
对象($diff
)中有大量其他与区间相关的信息,例如
$hours = $diff->h;
$minutes = $diff->i;
要为每个日期使用一天的开始,请在调用diff()
之前使用此内容...
$now->setTime(0, 0);
$eventTime->setTime(0, 0);
$diff = $now->diff($eventTime);
$days = $diff->d;
注意:如果您的日期间隔超过夏令时阈值
,您仍可能会遇到问题答案 1 :(得分:0)
你应该在进行分割之前休息一下,然后将地板()更精确地休息。 问题可能是楼层功能的调整。我会这样做:
floor(($eventTime-time())/(60*60*24))
答案 2 :(得分:0)
你可以使用strtotime函数获取任何日期的时间戳,如下所示
$current_time = strtotime(date('d-m-Y g:i:s A')); // timestamp of current date/time
$your_last_date = "10-Jan-2014";
$last_time = strtotime($your_last_date);
//subtract them
$sub_time = $current_time - $last_time;
// divide by timestamp of 1 day which is 60*60*24 = 86400;
// you will get the days
$days = floor($sub_time/86400); // no of days between previous date and now