lew1s Posted June 24, 2013 Share Posted June 24, 2013 Hi, I want to change this code to show time from current just for today. you can see on the webiste there is time from tomorrow but not from morning , but from current time. this is the code. can you please help ? http://www.dublincitycouriers.com/index2.php <label>Pickup Time</label> <select name="pickuptime"> <? $i=date('G', time()); $i++; while($i<18) { $date = gmmktime($i, 0, 0, gmdate("m") , gmdate("j",$selectDate), gmdate("Y")); if($date==$_SESSION['pickuptime']) { echo "<option selected value=\"".$i."\">".gmdate('H:00',$date)."</option>"; } else { echo "<option value=\"".$i."\">".gmdate('H:00',$date)."</option>"; } $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/ Share on other sites More sharing options...
requinix Posted June 24, 2013 Share Posted June 24, 2013 (edited) Going through date functions is overkill if you're just outputting hours. for ($i = 8; $i <= 18; $i++) { if ($i == $_SESSION['pickuptime']) { echo "<option selected value=\"" . $i . "\">" . sprintf('%02d', $i) . ":00</option>"; } else { echo "<option value=\"" . $i . "\">" . sprintf('%02d', $i) . ":00</option>"; } } Edited June 24, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1437705 Share on other sites More sharing options...
dalecosp Posted June 24, 2013 Share Posted June 24, 2013 Going through date functions is overkill if you're just outputting hours. He isn't just outputting hours, though. I'm not sure if I understand the question, OP. there is time from tomorrow but not from morning , but from current time Do you want the form to always start on "tomorrow morning"? Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1437707 Share on other sites More sharing options...
lew1s Posted June 26, 2013 Author Share Posted June 26, 2013 the thing is the time is showing right when you choose today. but when you choose tomorrow or another day its showing current time what is today. i want show for another day time from morning. hope its clear He isn't just outputting hours, though. I'm not sure if I understand the question, OP. Do you want the form to always start on "tomorrow morning"? Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1437978 Share on other sites More sharing options...
lew1s Posted June 26, 2013 Author Share Posted June 26, 2013 i applied your code but now the time for today is from morning. want to see from the time what time is. when is 16:00 there will be just option from 17:00 today. thanks for help Going through date functions is overkill if you're just outputting hours. for ($i = 8; $i <= 18; $i++) { if ($i == $_SESSION['pickuptime']) { echo "<option selected value=\"" . $i . "\">" . sprintf('%02d', $i) . ":00</option>"; } else { echo "<option value=\"" . $i . "\">" . sprintf('%02d', $i) . ":00</option>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1437982 Share on other sites More sharing options...
requinix Posted June 26, 2013 Share Posted June 26, 2013 Uh... you mean showing hours starting at the next hour? So at 7:01am it shows 8am+ and at 12:59pm it shows 1pm+? Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1438032 Share on other sites More sharing options...
kicken Posted June 26, 2013 Share Posted June 26, 2013 Near as I can tell, what you want is that for today, it shows the hours from now up until closing time. If the user changes the day to tomorrow or another future date though, you want to show all hours of the day, from open til close. You'll need some Javascript code to handle the case when the change the date. Your PHP code was fine as it was most likely since it worked properly for today, which is the default when the page loads. Add an onchange trigger <select name="pickupdate" onchange="updatePickupTime(this.selectedIndex)"> set an ID <select name="pickuptime" id="pickuptime"> Create the JS function. function updatePickupTime(idx){ var times = []; if (idx == 0){ //today var startHour = new Date().getHours()+1; for (; startHour < 18; startHour++){ times.push([startHour, startHour + ':00']); } } else { //other day for (var startHour = 8; startHour < 18; startHour++){ times.push([startHour, startHour + ':00']); } } var sel = document.getElementById('pickuptime'); sel.options.length=0; for (var i=0; i<times.length; i++){ var opt = document.createElement('option'); opt.value = times[i][0]; opt.text = times[i][1]; sel.appendChild(opt); } } That function is fairly basic, and you will probably need to modify it to suit your needs. Basically if the selected date is the first option, it gets the current hour and generates a list of times starting at (currentHour+1) until 18. If the selected option is one of the other options, it just generates a list of times from 8 to 18. Adjust to suit your desired range. Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1438112 Share on other sites More sharing options...
lew1s Posted June 27, 2013 Author Share Posted June 27, 2013 i think doing something wrong. can you help me please ? where to add the code and what to change . please . thanks! <? session_start(); //error_reporting(E_ALL); //ini_set('display_errors',true); /*$hostname="localhost"; $mysql_login="root"; $mysql_password=""; $database="onceoff_couriers"; if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){ $error=true; }else{ if (!(mysql_select_db("$database",$db))) { $error=true; } }*/ $hostname="sql4.hosting365.ie"; $mysql_login="onceoff_admin"; $mysql_password="df3tyus"; $database="onceoff_couriers"; if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){ $error=true; }else{ if (!(mysql_select_db("$database",$db))) { $error=true; } } include_once('inc/functions.php'); if(isset($_SESSION['pac'])){ $_POST['pickupaddress2']=$_SESSION['pac']; } if(isset($_SESSION['dac'])){ $_POST['dropoffaddress2']=$_SESSION['dac']; } if (isset($_POST['save'])) { mail($_POST['email'],"Courier Quote", "Thanks for visiting www.dublincitycouriers.com. Please find your saved quote" ); header("Location: send_email.php"); exit; } if(isset($_POST['submitform'])) { $labelerror = "style=\"color: red\""; $inputerror = "style=\"border-color: red\""; $display = "style=\"display: block\""; $displaynone = "style=\"display: none\""; $err = array(); $blank = 0; $_POST = array_map('stripslashes', $_POST); $escpost = array_map('mysql_real_escape_string', $_POST); foreach($_POST as $post=>$value) { if($post != 'pickupdate' && $post != 'pickuptime' && $post != 'pickupaddress2' && $post != 'pickupnotes' && $post != 'dropoffaddress2' && $post != 'dropoffnotes') { if(trim($value) == '') { $err[$post] = "blank"; } } } if(!isLetters($_POST['yourname']) && !isset($err['yourname'])) { $err['yourname'] = "<li>Your name must only contain letters and the following characters ,-'. </li>"; } if(!(isPhone($_POST['yourphone']) || isMobilePhone($_POST['yourphone'])) && !isset($err['yourphone'])) { $err['yourphone'] = "<li>You must enter a valid phone number.</li>"; } if(!checkEmail($_POST['youremail']) && !isset($err['youremail'])) { $err['youremail'] = "<li>You must enter a valid email.</li>"; } if(!isLetters($_POST['pickupname']) && !isset($err['pickupname'])) { $err['pickupname'] = "<li>Pickup name must only contain letters and the following characters ,-'.</li>"; } if(!(isPhone($_POST['pickupphone']) || isMobilePhone($_POST['pickupphone'])) && !isset($err['pickupphone'])) { $err['pickupphone'] = "<li>You must enter a valid pick up phone number.</li>"; } if(!isletters($_POST['pickupaddress1']) && !isset($err['pickupaddress1'])) { $err['pickupaddress1'] = "<li>You must enter a valid pick up address.</li>"; } if(!isLetters($_POST['dropoffname']) && !isset($err['dropoffname'])) { $err['dropoffname'] = "<li>Dropoff name must only contain letters and the following characters ,-'.</li>"; } if(!(isPhone($_POST['dropoffphone']) || isMobilePhone($_POST['dropoffphone'])) && !isset($err['dropoffphone'])) { $err['dropoffphone'] = "<li>You must enter a valid drop off phone number.</li>"; } if(!isLetters($_POST['dropoffaddress1']) && !isset($err['dropoffaddress1'])) { $err['dropoffaddress1'] = "<li>You must enter a valid drop off address.</li>"; } foreach($escpost as $postname => $postval) { $_SESSION[$postname] = $postval; } if(sizeof($err) == 0) { //header("Location: review.php"); redirect('review.php'); } } function redirect($url,$permanent = false) { if($permanent) { header('HTTP/1.1 301 Moved Permanently'); } header('Location: '.$url); exit(); } /*$check_email=true; $valid=true; $valid = $valid && isLetters($_POST['name']); $valid = $valid && checkEmail($_POST['email']); if($valid){ exit; } */?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Ireland's Online Same Day Delivery Service - OnceOffDeliveries</title><link rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css"><link href="css/style.css" rel="stylesheet" type="text/css"> <!--[if IE]><link rel="stylesheet" type="text/css" media="screen" href="css/style-ie.css" /><![endif]--><script src="js/jquery-1.3.2.js" type="text/javascript"></script><script src="js/jselect.js" type="text/javascript"></script><script>$(function() { $("form:not(.filter) :input:visible:enabled:first").focus();});</script><style type="text/css">html {background:#ccc;padding-top:50px;}</style> </head><body><div id="doc4" class="yui-t7 wrap"> <div id="header"> <div class="padding-10"> <div id="slogan"><h1>DUBLINCITYCOURIERS.com</h1><h2>Irelands Online Same Day Delivery Service</h2></div> <div id="call-us"><img src="images/tel.jpg" height="66" width="254" alt="onceoffdeliveries.ie"></div> <div class="clear"></div> <?php require_once('inc/menu.php'); ?> <div class="clear"></div> </div> </div> <div id="content-wrap"> <div class="padding-10"> <div class="yui-b"> <div id="breadcrumb"> <div id="quote_header"> <h2><a href="index.php">Quote</a> - Pickup / Delivery Details</h2> <div class="steps">Step 2 of 4</div> </div> <div class="errors" <?if(sizeof($err) > 0) echo $display; else echo $displaynone;?>> <ul> <? if(sizeof($err) > 0){ foreach($err as $errtype => $errval) { if($errval == "blank") { $blank++; } else { echo $errval; } } } if($blank > 0) { echo "<li>" . $blank . " fields were left blank</li>"; } ?> </ul> </div> </div> </div> <div class="yui-gb"> <form method="post" action="index2.php"> <div class="yui-u first"> <div id="your_details" style="border-left:0px;"> <div id="quote_header"> <h2>Your Details</h2> </div> <ul> <li> <label <?if(isset($err['yourname'])) echo $labelerror;?>>Contact Name</label> <input name="yourname" <?if(isset($err['yourname'])) echo $inputerror;?> value="<?=$_SESSION['yourname']?>"></input><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['yourphone'])) echo $labelerror;?>>Phone Number</label> <input name="yourphone" <?if(isset($err['yourphone'])) echo $inputerror;?> value="<?=$_SESSION['yourphone']?>" ></input><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['youremail'])) echo $labelerror;?>>Email</label> <input name="youremail" <?if(isset($err['youremail'])) echo $inputerror;?> value="<?=$_SESSION['youremail']?>"></input><font style="color:red"> *</font> </li> </ul> </div> </div> <div class="yui-u"> <div id="pickup_details"> <div id="quote_header"> <h2>Pickup Details</h2> </div> <ul> <li> <label <?if(isset($err['pickupname'])) echo $labelerror;?>>Contact Name</label> <input name="pickupname" <?if(isset($err['pickupname'])) echo $inputerror;?> value="<?=$_SESSION['pickupname']?>"></input><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['pickupphone'])) echo $labelerror;?>>Phone Number</label> <input name="pickupphone" <?if(isset($err['pickupphone'])) echo $inputerror;?> value="<?=$_SESSION['pickupphone']?>"></input><font style="color:red"> *</font> </li> <li> <label>Pickup Date</label> <select name="pickupdate"> <? for($i=0 ; $i<7 ; $i++) { $date = gmmktime($presentHour, 0, 0, gmdate("m"), gmdate("j")+$i , gmdate("Y")); $day=gmdate("w", $date); if(isset($_SESSION['pickupdate'])) { if($day!=0&&$day!=6) { if($i == $_SESSION['pickupdate']) { echo "<option selected value=\"".$i."\">".gmdate('D dS M',$date)."</option>"; } else { echo "<option value=\"".$i."\">".gmdate('D dS M',$date)."</option>"; } } } else { if($day!=0&&$day!=6) { if($date==$selectDate) { echo "<option selected value=\"".$i."\">".gmdate('D dS M',$date)."</option>"; } else { echo "<option value=\"".$i."\">".gmdate('D dS M',$date)."</option>"; } } } } ?> </select><font style="color:red"> *</font> </li> <li> <label>Pickup Time</label> <select name="pickuptime"> <? $i=date('G', time()); $i++; while($i<18) { $date = gmmktime($i, 0, 0, gmdate("m") , gmdate("j",$selectDate), gmdate("Y")); if($date==$_SESSION['pickuptime']) { echo "<option selected value=\"".$i."\">".gmdate('H:00',$date)."</option>"; } else { echo "<option value=\"".$i."\">".gmdate('H:00',$date)."</option>"; } $i++; } ?> </select><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['pickupaddress1'])) echo $labelerror;?>>Address Line 1</label> <input name="pickupaddress1" <?if(isset($err['pickupaddress1'])) echo $inputerror;?> value="<?=$_POST['pickupaddress1']?>"></input> </li> <li> <label <?if(isset($err['pickupaddress2'])) echo $labelerror;?>>Address Line 2</label> <input disabled name="pickupaddress2" <?if(isset($err['pickupaddress2'])) echo $inputerror;?> value="<?=$_POST['pickupaddress2']?>"></input> </li> <li> <label>Notes</label> <textarea name="pickupnotes"><?=$_SESSION['pickupnotes']?></textarea> </li> </ul> </div> </div> <div class="yui-u"> <div id="dropoff_details"> <div id="quote_header"> <h2>Dropoff Details</h2> </div> <ul> <li> <label <?if(isset($err['dropoffname'])) echo $labelerror;?>>Name</label> <input name="dropoffname" <?if(isset($err['dropoffname'])) echo $inputerror;?> value="<?=$_SESSION['dropoffname']?>"></input><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['dropoffphone'])) echo $labelerror;?>>Phone Number</label> <input name="dropoffphone" <?if(isset($err['dropoffphone'])) echo $inputerror;?> value="<?=$_SESSION['dropoffphone']?>"></input><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['dropoffaddress1'])) echo $labelerror;?>>Address Line 1</label> <input name="dropoffaddress1" <?if(isset($err['dropoffaddress1'])) echo $inputerror;?> value="<?=$_SESSION['dropoffaddress1']?>"></input><font style="color:red"> *</font> </li> <li> <label <?if(isset($err['dropoffaddress2'])) echo $labelerror;?>>Address Line 2</label> <input disabled name="dropoffaddress2" <?if(isset($err['dropoffaddress2'])) echo $inputerror;?> value="<?=$_POST['dropoffaddress2']?>"></input> </li> <li> <label>Notes</label> <textarea name="dropoffnotes"><?=$_SESSION['dropoffnotes']?></textarea> </li> </ul> </div> <div id="proceed"> <input type="submit" name="submitform" value="Next >>"></input> </div> </form> </div> </div> </div> </div> <?php include_once("inc/footer.php") ?></div><?php include_once("inc/googleanalytics.php") ?></body></html> Near as I can tell, what you want is that for today, it shows the hours from now up until closing time. If the user changes the day to tomorrow or another future date though, you want to show all hours of the day, from open til close.You'll need some Javascript code to handle the case when the change the date. Your PHP code was fine as it was most likely since it worked properly for today, which is the default when the page loads.Add an onchange trigger <select name="pickupdate" onchange="updatePickupTime(this.selectedIndex)"> set an ID <select name="pickuptime" id="pickuptime"> Create the JS function. function updatePickupTime(idx){ var times = []; if (idx == 0){ //today var startHour = new Date().getHours()+1; for (; startHour < 18; startHour++){ times.push([startHour, startHour + ':00']); } } else { //other day for (var startHour = 8; startHour < 18; startHour++){ times.push([startHour, startHour + ':00']); } } var sel = document.getElementById('pickuptime'); sel.options.length=0; for (var i=0; i<times.length; i++){ var opt = document.createElement('option'); opt.value = times[i][0]; opt.text = times[i][1]; sel.appendChild(opt); } } That function is fairly basic, and you will probably need to modify it to suit your needs. Basically if the selected date is the first option, it gets the current hour and generates a list of times starting at (currentHour+1) until 18. If the selected option is one of the other options, it just generates a list of times from 8 to 18. Adjust to suit your desired range. Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1438169 Share on other sites More sharing options...
lew1s Posted July 1, 2013 Author Share Posted July 1, 2013 anybody help? Quote Link to comment https://forums.phpfreaks.com/topic/279505-pickup-time-date/#findComment-1438771 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.