Reputation: 791
I am sending reservation info from php to javascript via post. Using floor($eventTime/(60*60*24)) - floor(time()/(60*60*24));
I tell my javascript code where to put my events based on what date they have. $eventTime
is a unix timestamp. E.x. any date on the 21st should go in box one, any day on the 22nd should go in box 2, etc. What am I doing wrong? The time in hours is affecting which day the reservation is displayed in, events on the 19 and 20th are mixed, etc but I use floor at the number in days so I don't understand. To be clear, I want the number of days from now to another date but not including or considering hours/minutes. So if today is the 10th, any reservation on the 10th no matter what time (00:00:00 - 23:59:59) should have it's "timeFrom" variable represented as 0. What is happening is dates on the 10th and 11th in this example are both having 0 sent as the "days between" variable when the 10th should be 0 and the 11th should be one.
here's the relevant 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 .= "!";
}
and the relevant 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;
}
}
});
}
Upvotes: 0
Views: 93
Reputation: 164776
Easiest way to get the time diff in days is to use 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;
Of course, this will only get you the actual number of full days between the two date / times. There's plenty of other interval related information in the DateInterval
object ($diff
), eg
$hours = $diff->h;
$minutes = $diff->i;
To work with the start of the day for each date, use this before calling diff()
...
$now->setTime(0, 0);
$eventTime->setTime(0, 0);
$diff = $now->diff($eventTime);
$days = $diff->d;
Note: You may still run into problems if your date interval crosses a daylight savings threshold
Upvotes: 1
Reputation: 9635
you can user strtotime function to get timestamp of any date like this
$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
Upvotes: 0
Reputation: 36
You should rest the times before doing the division and the floor() to be more precise. The problem could be the adjustments that the floor function make. I would make in this way:
floor(($eventTime-time())/(60*60*24))
Upvotes: 0