I feel like I'm missing something easy here and getting a little stuck while building a "schedule an appointment" calendar.
Clients can book their own time slots, and when they do...I need to make those time slots unavailable to others.
I am using a drop down to "for loop" in 30 minute intervals from 9am - 7pm for the "start time" selection. If a "start time" of "10:00am" and an "end time" of "11:30am" are saved in the database...10:00am, 10:30am, and 11:00am need to be withheld from the "start time" dropdown after successful form submission.
(Database results are in the form of a string...fyi)
To do that, I have this:
$sql_slots = "SELECT * FROM XXXXXXXXX WHERE date = '$query_date' ORDER BY XXXXXXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
echo'
<select name="start_time">';
for($b = 0; $b <= 20; $b++){
$y = strtotime(($b*30) . " Minutes", $open);
$the_time = date("h:ia", $y) . "<br>";
$starting = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
$starting[] = $row_result_slots['start_time'];
}
if(in_array($the_time, $starting)){
echo "<option>Not Available</option>";
} else {
echo "<option>" . $the_time . "</option>";
}
}
echo'</select>';
Any suggestions, to finish this or a better method than "in_array" to accomplish what I'm trying to do?
Thank you in advance.
The solution was to move the "while loop" outside of the "for loop" and I wasn't taking into account the "br" attached to the end of the $the_time variable.
Here is the working code:
$sql_slots = "SELECT * FROM XXXXXX WHERE date = '$query_date' ORDER BY XXXXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
$starting = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
$starting[] = $row_result_slots['start_time'];
}
echo'
<select name="start_time">';
for($b = 0; $b <= 20; $b++){
$y = strtotime(($b*30) . " Minutes", $open);
$the_time = date("h:ia", $y);
if(in_array($the_time, $starting)){
echo "<option>Not Available<br></option>";
} else {
echo "<option>" . $the_time . "<br></option>";
}
}
Related
We are creating a system where employees need to update their daily status report. The fundamental purpose of the system is to note the missing dates on which they did not update the status report.
I have found a way to do that by checking the day difference between the 2 array values and then counting & displaying the days. However, I am not sure how to do this dynamically between the 2 array values.
Here's the code I have used along with the output:
//id of the person updating the DSR
$userid = $_id;
// Array to fetch all the DSR by specific user
$fine = getDSRbyUserIdOrderDate($userid);
$today = date('d-m-Y');
$tomorrow = date("d-m-Y", strtotime($today . " +1 day"));
$ok = count($fine) - 1;
//Array to get dates
$d1 = $fine[0]['date'];
$d2 = $fine[1]['date'];
$d3 = $fine[2]['date'];
// Function call to find date difference
$dateDiff = dateDiffInDays($d1, $d2);
$dateDiff1 = dateDiffInDays($d2, $d3);
echo "<h4 style='color:red'>You have missed the DSR on the following dates.</h4>";
for($p = 1; $p < $dateDiff; $p++){
$missingdate = date("d-m-Y", strtotime($d1 . " +$p day"));
echo "<span style='color:red'>$missingdate</span>";
echo "<br />";
}
for($p = 1; $p < $dateDiff1; $p++){
$missingdate = date("d-m-Y", strtotime($d2 . " +$p day"));
echo "<span style='color:red'>$missingdate</span>";
echo "<br />";
}
if($d2 != $today){
echo "<span style='color:red'>$today <i>- Kindly update DSR before midnight</i></span> ";
echo "<br />";
}
Output:
I would create first a list of entries by date and then "paint" it accordingly.
$starting_date = new DateTime('2019-11-23');
$num_days = 10
$from_db_by_date = [];
foreach ($fine as $entry) {
$from_db_by_date[$entry['date']] = $entry;
}
$all_by_date = [];
for ($i = 0; $i < $num_days; $i++) {
$date_str = $starting_date->format('d-m-Y');
$all_by_date[$date_str] = $from_db_by_date[$date_str] ?: null;
$starting_date->modify('+1 day');
}
Now you can loop through $all_by_date, check if the entry is null and act accordingly.
I'm trying to create an array of dates. The idea is that I add a number x days and the code, when adding the days skip only Sunday.
This is for laravel and I'm using carbon.
$date = Carbon::now();
$dates = [];
for($i = 1 ; $i < 20; $i++){
if($date->dayOfWeek === Carbon::SATURDAY){
echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . " - Sunday <br> ";
} else {
echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . "<br>";
}
When i use the constant SUNDAY to skip this date, its not working.
It goes on to consider Sunday as Monday
The problem is that you are checking if it's Saturday, and after that you're adding a day to it.
You need to echo the date before you add a day to it.
Try this:
if($date->dayOfWeek === Carbon::SUNDAY){ // checking if the current date is a sunday
echo $dates[$i] = $date->format('d/m/Y') . " - Sunday <br> "; // echo and add the current date to the array
$date->addDay(1);
} else {
echo $dates[$i] = $date->format('d/m/Y') . "<br>"; // echo and add the current date to the array
$date->addDay(1);
}
I got it with this code:
$inicialDate = Carbon::now();
$newDate = [];
for($i = 1; $i < 30; $i++)
{
$newDate[$i] = $inicialDate->addDay(1);
if($newDate[$i]->format('l') == "Sunday")
{
$newDate[$i] = $inicialDate->addDay(1);
}
echo $newDate[$i]->format('d/m/Y') . " - " . $newDate[$i]->format('l') . "<br>";
}
i'm trying to do a contract management app using php and mysql and i'm having some questions regarding the dates.
I need to know the time that there is between today and specific dates in the contract, or if there is less than a month it should display days left..
the problem is that the comparison to know if the end of contract is in the past or in the future, does'n seems to work!
link to check the code: link to project
$hoje = date_create();
$fim = '2022-11-11';
$fim_data = date_create($fim);
$diff = date_diff( $hoje, $fim_data );
$meses = (($diff->format('%y')*12)+$diff->format('%m'));
$dias = $diff->days;
var_dump($fim < $hoje);
if($fim < $hoje) {
$result = "Contract has ended";
} elseif($meses >=1 ) {
$result = $meses . " months";
echo '<br>';
} else {
$result = $dias . " days";
};
echo '<br>';
echo $result;
You are comparing string with date object
Replace
if($fim < $hoje) {
With
if($fim_data < $hoje) {
Corrected code with solution by Felippe Duarte
$hoje = date_create();
$fim = '2018-11-11';
$fim_data = date_create($fim);
$diff = date_diff( $hoje, $fim_data );
$meses = (($diff->format('%y')*12)+$diff->format('%m'));
$dias = $diff->days;
var_dump($fim_data < $hoje);
if($fim_data < $hoje){$result = "não aplicavel";}
elseif($meses >=1 ){
$result = $meses . " meses";
echo '<br>';}
else{
$result = $dias . " dias";};
echo '<br>';
echo $result;
I have a website that reads data about baseball games. First the website displays the game dates and scores:
$.post('../php/getGameDates.php', function(returnedDates) {
var objDates = jQuery.parseJSON( returnedDates );
$('#content').hide();
var pubStr = "";
for (var a=0; a<objDates.length; a++) {
var dateParts = objDates[a].game_date.split("-");
var mo;
switch(dateParts[1]) {
case "04":
mo = "April"
break;
case "05":
mo = "May"
break;
case "06":
mo = "June"
break;
case "07":
mo = "July"
break;
case "08":
mo = "Aug."
break;
case "09":
mo = "Sept."
break;
case "10":
mo = "Oct."
break;
default:
break;
}
var day = dateParts[2].replace(/^0+/, '');
pubStr += "<div class='game_to_click' id='" + objDates[a].game_date + "'>" + mo + " " + day + ", " + dateParts[0] + ": " + objDates[a].score + "</div>"
}
$('#game_dates').append(pubStr);
...
});
When you click a date, you get a popup of data about that game. There are prev/next buttons on the popup. The thing is, the data seems to "blink" when it appears on the popup. I suspect that's because of the query to the database.
Here is the php code for the "prev" button:
<?php
include_once ('../../../homicide/php/constants_test.php');
// connect to database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //open db conn
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
//date_default_timezone_set('America/New_York');
$thisDate = $_POST['thisDate'];
//echo $thisDate;
//$time = strtotime($thisDate . ' - 1 day');
//$newDate = date('Y-m-d',$time);
$parts = explode("-", $thisDate);
$day = $parts[2];
if (substr($day, -2) == "0") {
$day = substr($day, -1);
$day = intval($day);
}
$day--;
$newDate = $parts[0] . "-" . $parts[1] . "-";
//echo '$day: ' . $day;
if (strlen($day) < 2){
$newDate .= "0";
}
$newDate .= $day;
//echo "new: " . $newDate . " ";
tryQuery($newDate, $mysqli);
function tryQuery($thisDate, $mysqli) {
$q = "SELECT * FROM pirates_games where game_date = '" . $thisDate . "'";
$result = $mysqli->query($q);
$row_cnt = mysqli_num_rows($result);
//echo $row_cnt;
if ($row_cnt > 0) {
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$arrGame = $row;
}
echo json_encode($arrGame);
mysqli_close($mysqli);
}
else {
//echo 'date on entry: ' . $thisDate . " ";
$parts = explode("-", $thisDate);
$day = $parts[2];
if (substr($day, -2) == "0") {
$day = substr($day, -1);
$day = intval($day);
}
$day--;
$newDate = $parts[0] . "-" . $parts[1] . "-";
//echo '$day: ' . $day;
if (strlen($day) < 2){
$newDate .= "0";
}
$newDate .= $day;
//echo "new: " . $newDate . " ";
//$time = strtotime($thisDate . ' - 1 day');
//$newDate = date('Y-m-d',$time);
//echo "new: " . $newDate;
tryQuery($newDate, $mysqli);
}
}
?>
Is this method of trying first one query then another the right way to go about this? Most times, there is a game the next day or the previous day, but sometimes, the dates skip a day. I'm not sure how to account for skipped days when I try to find the next or previous game.
My way of thinking on this would be to load the games as a sequential rather than a date based method. Then you could recode to show previous 'game' rather than 'previous day's game', (or next day's game) etc... It's more accurate to the game sequence you've described. This way the previous game's date is just associated information, but it could easily be part of the display.
If that doesn't appeal to you, you could load the dates into an array with the associated games (date being the key and game or no game being the data). On dates that have no game you can show that in your display options.
I didn't articulate my problem very well, but I was having two problems at once. Part of the problem was click events firing multiple times, for both the next and prev buttons (See
jQuery click events firing multiple times):
$(".next").unbind().click(function() {
//Stuff
});
$(".prev").unbind().click(function() {
//Stuff
});
The other part of the problem was finding what the next or previous date was, and here, #glennw (below) was correct.
$array_of_time = array ();
$start_time = strtotime ("$app_date 07:00");
$end_time = strtotime ("$app_date 22:00");
$mins = 04 * 60;
while ($start_time <= $end_time)
{
$array_of_time[] = date ('h:i a', $start_time);
$start_time += $mins;
}
echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";
foreach($array_of_time as $key=>$value)
{
for($i = 0; $i < count($app_data); $i++)
{
$book_time=$app_data[$i]["appointment_time"];
if($value==$book_time)
{
echo "<a class='time_slot_a_book'>$value</a> ";
} else {
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
}
echo "</div>";
Here foreach loop can run as many time as it can as well as for loop also, but i want to show the links that are not matched with the foreach value and for loop value.
The foreach loop values like 7:20 am not from database but the for loop value like 7:20 am is from database so if 7:20 am==7:20 am then the if statement it run it is working fine, but the issue is that it is running 2 time if i get 2 value in for loop. It should run my div only once.
If took a tour of your site, I revise the copy:
$array_of_time = array ();
$start_time = strtotime ("$app_date 07:00");
$end_time = strtotime ("$app_date 22:00");
$mins = 04 * 60;
while ($start_time <= $end_time)
{
$array_of_time[] = date ('h:i a', $start_time);
$start_time += $mins;
}
echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";
foreach($array_of_time as $key=>$value)
{
//
// get the appointing state:
//
$bAppointed = false;
for($i = 0; $i < count($app_data); $i++)
{
$book_time = $app_data[$i]["appointment_time"];
$bAppointed = $value == $book_time;
if($bAppointed)
{
break;
}
}
//
// print the time slot:
//
if($bAppointed)
{
echo "<a class='time_slot_a_book'>$value</a> ";
} else {
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
echo "</div>";
?
Is it better ?