PHP - Formatting Daily Hours
I have a weird issue. I'm working on something where people will submit hours for a business. Originally it was free form text, but then I figured it may get messy with people doing M-F 9-5 then Monday through Friday, 9AM to 5PM, Mon-Fri, etc. So I switched to a bunch of inputs. It has each day then a select for from and one for to. Data is passed to the script as a form submission. Vars are named HF# and HT# (hours from, hours to and a 0-6 digit for Mon-Sun). I want to take the input and output a string as such:
Mon-Tue, 8:00 AM to 5:00 PM
Thu-Fri, 8:00 AM to 5:00 PM
Sat, 9:00 AM to 1:00 PM
Sun, 12:00 PM to 3:00 PM
I did this:
$formdata = array(); $formdata["hf0"] = "8:00 AM"; $formdata["ht0"] = "4:30 PM"; $formdata["hf1"] = "8:00 AM"; $formdata["ht1"] = "4:30 PM"; $formdata["hf2"] = ""; $formdata["ht2"] = ""; $formdata["hf3"] = "8:00 AM"; $formdata["ht3"] = "4:30 PM"; $formdata["hf4"] = "8:00 AM"; $formdata["ht4"] = "4:30 PM"; $formdata["hf5"] = "9:00 AM"; $formdata["ht5"] = "1:00 PM"; $formdata["hf6"] = "12:00 PM"; $formdata["ht6"] = "3:00 PM"; $WeekDays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); $hours = ""; $sd = "Mon"; $ld = ""; $fr = $formdata["hf0"]; $to = $formdata["ht0"]; for ($x=0; $x<=6; $x++) { if (empty($sd) && !empty($formdata["hf$x"])) { $sd = $WeekDays[$x]; $fr = $formdata["hf$x"]; $to = $formdata["ht$x"]; } if ($fr != $formdata["hf$x"] || $to != $formdata["ht$x"]) { $hours .= "$sd" . (($sd==$ld)?", ":" - $ld, ") . " $fr to $to<br/>"; if (!empty($formdata["hf$x"])) { $sd = $WeekDays[$x]; $fr = $formdata["hf$x"]; $to = $formdata["ht$x"]; } else { $sd = ""; } } $ld = $WeekDays[$x]; }($formdata was because I was tired of hitting back, changing the data then resubmitting) This kinda works, but it skips Sunday. There has got to be a better way to do this.. any ideas? Similar TutorialsHi, I can't think how to make a counter which goes up during the day, and when a new day starts it gets reset to 0. How can I do this without relying on visitors to the site to run the script? Cheers, Tom. Im looking for some example code for countdowns. I need to be able to count down to the same time each day for example 9PM each day. and be displayed as 5hr 8m 15s. When the timer runs out, then run some code then start again for the next day. what would be the best route to take for this? Thanks in advance Hi, This is my first post so dont kill me if i did somthing wrong. Im trying to make a simple php/javascript page that will display the time remaining in each period every day (we have 4 periods per day). I found a nice javascript library from www.hashemian.com and am using the example that he linked to to do multiple countdown timers on one page. The problem i am having is that the only way i can think of to make it so that it counts down to a dynamic date is to specify that date as a variable and then combine it with the string that specifys the rest of the date/time combo for the target date/time. The current live version is at smd75jr.com/test/index2.php The first timer is just a test that im using to make sure i havent completely broken it. THe second timer is the one im trying to troubleshoot, it is currently set for 11:59 PM EST "today". (see code below) Any help would be greatly appreciated!! This is my code: Code: [Select] <html> <head> <title>Multiple Countdown Clocks</title> </head> <body> <div id="clock1">[clock1]</div> <div id="clock2">[clock2]</div> </body> <script language="JavaScript"> StartCountDown("clock1","06/27/2012 9:33 PM -0400") StartCountDown("clock2","periodTest") TodaysDate() //var today = new Date() //var todayMonth = today.getMonth() + 1 //var todayDay = today.getDate() //var todayYear = today.getFullYear() //var todayDate = (todayMonth + "/" + todayDay + "/" + todayYear) var periodTest = (todayDate + " 11:59 PM -0400") var periodA1 = (todayDate + " 8:53 AM -0400") var periodA2 = (todayDate + " 10:26 AM -0400") var periodA31 = (todayDate + " 12:32 PM -0400") var periodLunch1 = (todayDate + " 11:00 AM -0400") var periodLunch2 = (todayDate + " 12:32 PM -0400") var periodA32 = (todayDate + " 11:59 AM -0400") var periodA4 = (todayDate + " 2:05 PM -0400") //function Periods(todayDate, periodA1, periodA2, periodA31, periodA32, periodA4, periodB1, periodB2, periodB31, periodB32, periodB4, periodLunch1, periodLunch2, periodSchoolStart, periodSchoolEnd) // { // var today = new Date() // var todayMonth = today.getMonth() + 1 // var todayDay = today.getDate() // var todayYear = today.getFullYear() // var todayDate = (todayMonth + "/" + todayDay + "/" + todayYear) // // var periodA1 = (todayDate + " 21:25 PM -0400") // } function TodaysDate(todayDate) { var today = new Date() var todayMonth = today.getMonth() + 1 var todayDay = today.getDate() var todayYear = today.getFullYear() var todayDate = (todayMonth + "/" + todayDay + "/" + todayYear) } /* Author: Robert Hashemian (http://www.hashemian.com/) Modified by: Munsifali Rashid (http://www.munit.co.uk/) Modified by: Tilesh Khatri */ function StartCountDown(myDiv,myTargetDate) { var dthen = new Date(myTargetDate); var dnow = new Date(); ddiff = new Date(dthen-dnow); gsecs = Math.floor(ddiff.valueOf()/1000); CountBack(myDiv,gsecs); } function Calcage(secs, num1, num2) { s = ((Math.floor(secs/num1))%num2).toString(); if (s.length < 2) { s = "0" + s; } return (s); } function CountBack(myDiv, secs) { var DisplayStr; var DisplayFormat = "%%D%% Days %%H%%:%%M%%:%%S%%"; DisplayStr = DisplayFormat.replace(/%%D%%/g, Calcage(secs,86400,100000)); DisplayStr = DisplayStr.replace(/%%H%%/g, Calcage(secs,3600,24)); DisplayStr = DisplayStr.replace(/%%M%%/g, Calcage(secs,60,60)); DisplayStr = DisplayStr.replace(/%%S%%/g, Calcage(secs,1,60)); if(secs > 0) { document.getElementById(myDiv).innerHTML = DisplayStr; setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ");", 990); } else { document.getElementById(myDiv).innerHTML = "Period Over"; } } </script> </html> Hi... I planned to create db table daily. When day begin button clicked (text box has date Value) master dB can be copied,with the name of text box value ($date). If already day begin then it show day begin done. How can I do that. Please guide me
hi i have daily data table it's contain ID, Date, and Rain i need to make a new table but tendays period ,,, any clues ,,, will be very greatfull thanks a lot ,,, Hey everyone. Currently I have a site set up where I manually edit the txt file and the site pulls it from the text file and displays via php. I wanted to automate it by creating one file which contains the sunset times for the whole year, and have php determine todays date and then display the corresponding sunset time. Can someone please point me in the right direction? maybe it can be set up in a way where each line in the txt file represents the day of the year. ex: Jan 1 would be line 0, Feb 1's sunset time would be on line 31 etc. Thanks Hey all, Im currently working on a site which I save a cookie on users computer for 1 month as well a row in a table with the 1 month expiring date plus cookie id. What I would like to know is if theres a way to make the server check the table every day at say 12pm to see if any corresponding user cookies in each row have expired on that day as to remove them from the table to match the expiring cookie on the users computer? I would like not to have to rely on a user/admin accessing in to make a script run instead have something thats timed to go off automatically instead? Is there anyway of doing this? Thanks! Hello I am trying to do a cronjob to check if a user has been online within the past 24 hours. I have a column with "last_login" with this format: "1337593284". How can I do a simple if (last_login+24hours< servertime) { do something... I am trying to make a function that is customizable to delete a query from a database. Here's the function. function checkHashs($hours) { if($getHashs = mysql_query('SELECT * FROM `hash_codes`')) { $dateQ = mysql_query('SELECT MAX(addedon) AS lastVoteDate FROM `hash_codes`') or die(mysql_error()); $getDate = mysql_fetch_assoc($dateQ); $diffrence = time() - strtotime($getDate['lastVoteDate']); echo $diffrence / 60 / 60; if (($diffrence / 60 / 60) >= $hours) { mysql_query('DELETE FROM `hash_codes` WHERE `addedon` = '. ($diffrence / 60 / 60) >= $hours .'') or die(mysql_error()); } } } And I am trying to call it by checkHashs(24); What I am trying to do is make it so when that function is ran it will delete all oh the guerys that have the `addedon` row over X hours(24 hours in this case). Any ideas? in my MySql database i have a field "created_at" = now() for ex: 2011-05-23 11:47:28 i need to add two hours to that so i have 2011-05-23 13:47:28 how will i do this please? i have this so far but it is not correct: $currentTime = time($msg->getCreatedAt()); $timeAfterOneHour = $currentTime+60*60; $date=date("Y-m-d H:i:s",$timeAfterOneHour); please help thank you Im using $time = date('Y-m-d H:i:s'); How can I make a new variable that adds 4 hours on the $time output. Hi, I have a row In mysql data base called time and the content is filled with the time function time (); I want to know is there a way to achieve this I want the entire column deleted if it has been 24 hours since the time row was filled. I want this to happen automatically I don't really know how to achieve this so any help would be much appreciated thanks. Hi, I have a database where I store times with the Now(); function. What I want is: When the viewer looks at the post, instead of it saying: Posted: 2010-11-14 19:08:14, I want it to say posted: x hours ago, Posted: x days ago, etc. Any help with this would be appreciated! I would like to create function, what prints times to specified day. For example Day time total 1 10 - 12 2 2 0 (or nothing) 3 10 - 12:15 2:15 4 10:30 - 12 1:30 5 10 - 12 2 6 0 (or nothing) 7 0 (or nothing) Time comes from database and there is column for day, starting time (like 10), ending time (like 12:15) and total, calculated, time in db. So day and times come from db. Have thinked this much, but can't figure out how. Hello, I have a very simple table... DAY (Y-m-d) / TIME (00:00:00) / POWER (INT) I am using a Jquery datepicker to select the date on the page, and that POST to the PHP file. I am trying to select values from Mysql to make 3 HighCharts Graphs using the selected day of the datepicker. I have started with the DAY graph PHP to get the values for each hour of a 24 hour day. I need to get the values for each hour... 23,12,15,35 etc... , and then I need for the 31 days of a month for the month graph, and 12 months of the year for the month graph all in the same way so the HighCharts can use the values to make the chart (3 php files, one for each graph) Here is the PHP I have for the 1 day that should get the 24 individual hour data, but it does not seem to work... <?php $choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : date("Y-m-d"); $con = mysql_connect("localhost","root","mackie1604"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("inverters", $con); $sql = "SELECT HOUR(time), COUNT(power) FROM feed WHERE time = '".$choice."' GROUP BY HOUR(time) ORDER BY HOUR(time) "; $res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error()); $row = mysql_fetch_assoc($res); echo $row['choice'].'<br />'; ?> What have a written wrong in the sql query ??? Alan It was suppose to be my day off but here I set in front of the computer trying to figure out some code. I can not get my code to send me the information that is inputted into the fields. The style is what I want but most importantly it has got to work. I'm going to put the PHP code and the HTML code. If you need the CSS code I can post that. I'm not sure if you guys need that though. Hopefully someone out there can help me out. I know it might be a very simple error causing me tons of problems. I just can't figure it out. I'm novice at this stuff. PHP CODE & HTML CODE: <?php if ($_POST["email"]<>'') { $ToEmail = 'Justin@fullloop.net'; $emailSubject = 'contact'; $mailheader = "From: ".$_POST["email"]."\r\n"; $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; $MESSAGE_BODY = "Phone: ".$_POST["phone"]."<br>"; $MESSAGE_BODY .= "Your Email: ".$_POST["email"]."<br>"; $MESSAGE_BODY .= "Your Message: ".nl2br($_POST["comment"])."<br>"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> <html> <body> <?php echo $_POST["name"]; ?>,<br /> Thank you for contacting Full Loop Inspections. We take great pride in our services and we are grateful you are considering us to complete your inspection. An inspector will contact you at <?php echo $_POST["phone"]; ?> or email you at <?php echo $_POST["email"]; ?> . </body> </html> <?php } else { ?> <form id="contacts-form" method="post" action="contact.php"> <fieldset> <div class="field"> <label>Your Name:</label> <input type="text" value="" name="name"/> </div> <div class="field"> <label>Your Phone:</label> <input type="text" value="" name="phone"/> </div> <div class="field"> <label>Your E-mail:</label> <input type="text" value="" name="text"/> </div> <div class="field"> <label>Your Message:</label> <textarea cols="1" rows="1"></textarea> </div> <div class="wrapper"><a href="contact-us.html" onClick="document.getElementById('contacts-form').submit()" class="link1"><em><b>SUBMIT</b></em></a></div> </fieldset> </form> <?php }; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head profile="http://www.w3.org/2005/10/profile"> <link rel="icon" type="image/gif" href="images/favicon.gif" /> <head> <title>Full Loop Inspections | Columbus Ohio #1 Home Inspector | Home Inspectors | Commercial | Mold | Grove City </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="Certified inspectors performing commercial and home inspections in Columbus, Grove City, Hilliard, Gahanna and surrounding areas" /> <meta name="keywords" content="Columbus home inspectors, Grove City Inspectors, Home Inspections, Home Inspector" /> <meta name="author" content="Full Loop Inspections | Home | Commercial | Mold | Radon | Mold Inspectors - http://www.fullloop.net" /> <link href="style.css" rel="stylesheet" type="text/css" /> <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="js/cufon-yui.js" type="text/javascript"></script> <script src="js/cufon-replace.js" type="text/javascript"></script> <script src="js/Myriad_Pro_400.font.js" type="text/javascript"></script> <script src="js/Myriad_Pro_600.font.js" type="text/javascript"></script> <script src="js/NewsGoth_BT_400.font.js" type="text/javascript"></script> <script src="js/NewsGoth_BT_700.font.js" type="text/javascript"></script> <script src="js/NewsGoth_Dm_BT_400.font.js" type="text/javascript"></script> <script src="js/script.js" type="text/javascript"></script> <!--[if lt IE 7]> <script type="text/javascript" src="js/ie_png.js"></script> <script type="text/javascript"> ie_png.fix('.png, #header .row-2 ul li a, .extra img, #search-form a, #search-form a em, #login-form .field1 a, #login-form .field1 a em, #login-form .field1 a b'); </script> <link href="ie6.css" rel="stylesheet" type="text/css" /> <![endif]--> <script type="text/javascript"> (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-24965394-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script> </head> <body id="page5"> <div id="main"> <!-- HEADER --> <div id="header"> <div class="row-1"> <div class="fleft"><a href="home.html"><img src="images/logo.png" alt="full-loop-inspections" /></a></div> <div class="fright"> <div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=146395285448812&xfbml=1"></script><fb:like href="http://www.facebook.com/pages/Full-Loop-Inspections/237677882939676" send="true" layout="button_count" width="150" show_faces="true" action="like" font=""></fb:like> <g:plusone size="small"></g:plusone> <ul> <li><a href="home.html"><img src="images/icon1.gif" alt="home-inspections" /></a></li> <li><a href="contact-us.html"><img src="images/icon2-act.gif" alt="contact-a-home-inspector" /></a></li> <li><a href="home-inspectors-service-area.html"><img src="images/icon3.gif" alt="home-inspectors-sitemap" /></a></li> </ul> </div> </div> <div class="row-2"> <div class="left"> <ul> <li><a href="home.html"><span>home</span></a></li> <li><a href="inspection-services.html"><span>inspection services</span></a></li> <li><a href="payments.html"><span>Make Payments</span></a></li> <li><a href="contact-us.html" class="active"><span>contact</span></a></li> <li class="last"><a href="home-inspectors-service-area.html"><span>Service Area</span></a></li> </ul> </div> </div> <div class="row-3"> <div class="inside"> <h2>Columbus Ohio<b>Inspections</b></h2> <p>Full Loop Inspections services Columbus Ohio and surrounding cities. Franklin, Madison, Union, Delaware, Licking, Pickaway and Fairfield counties. Contact us today to set an Inspection appointment. </p> </div> </div> <div class="extra"><img src="images/header-img.png" alt="Columbus-Ohio-Inspectors" /></div> </div> <!-- CONTENT --> <div id="content"><div class="inner_copy">More <a href="http://www.templatemonster.com/">Website Templates</a> at TemplateMonster.com!</div> <div class="box"> <div class="border-bot"> <div class="right-bot-corner"> <div class="left-bot-corner"> <div class="inner"> <div class="box1 alt"> <div class="inner"> <h4><b>Our</b> Contacts</h4> <div class="address"> <p><b>Inspector:</b> Justin Ashley<br /> <b>Telephone:</b>614-256-6505<br /> <b>Address:</b>4690 Helmsbridge Ct<br /> <b>City:</b>Grove City, OH 43123<br /> <b>e-mail:</b><a href="mailto:Justin@Fullloop.net?subject=Online Inspection Referral">Justin@Fullloop.net</a></p> </div> <p class="p0 extra-wrap"><br /> <span class="img-box1"><img src="images/InterNACHI.png" alt="InterNachi-Home-Inspectors" width="81" height="70" /></span><span class="img-box1"><img src="images/Infrared_Certified_Small.png" alt="Thermal-Imaging-home-inspector" width="57" height="70" /></span><span class="img-box1"><img src="images/IAC_Mold_Radon.png" alt="mold-inspector" width="94" height="70" /><img src="images/Columbus-board-of-realtors.jpg" alt="mold-inspector" width="70" height="70" /></span></p> </div> </div> </div> </div> </div> </div> </div> <div class="indent"> <div class="wrapper"> <div class="col-1"> <h3><b>Contact</b> Form</h3> <form id="contacts-form" method="post" action="contact.php"> <fieldset> <div class="field"> <label>Your Name:</label> <input type="text" value="" name="name"/> </div> <div class="field"> <label>Your Phone:</label> <input type="text" value="" name="phone"/> </div> <div class="field"> <label>Your E-mail:</label> <input type="text" value="" name="text"/> </div> <div class="field"> <label>Your Message:</label> <textarea cols="1" rows="1"></textarea> </div> <div class="wrapper"><a href="contact-us.html" onClick="document.getElementById('contacts-form').submit()" class="link1"><em><b>SUBMIT</b></em></a></div> </fieldset> </form> </div> <div class="col-2"> <div class="box2"> <div class="border-top"> <div class="left-top-corner"> <div class="right-top-corner"> <div class="inner"> <h4><b>Property </b>Info</h4> <ul class="news"> <li><a href="full-loop-inspections">Sept 13, 2011</a> <p>When contacting us please provide as much information as you can about the property.</p></li> <li><a href="full-loop-inspections">Sept 13, 2011</a> <p>We will take the time to answer any questions you might have. </p></li> </ul> </div> </div> </div> </div> </div> <div class="box3"> <div class="right-bot-corner"> <div class="left-bot-corner"> <div class="inner"> <center> <p> </p> <p> </p> <p><img src="images/JPicture.jpg" alt="Home-Inspectors-Columbus" /></p> <p> <strong><font color="#FFFFFF">Full Loop Inspections<br /> Certified Home Inspector<br /> Justin Ashley<br /> 4690 Helmsbridge Ct<br /> Grove City, OH 43123<br /> (614)-256-6505</strong><br /><a href="mailto:Justin@Fullloop.net?subject=Inspection"><font color="#000000"> Justin@Fullloop.net</font></a></p> </center> </div> </div> </div> </div> </div> </div> </div> </div> <!-- FOOTER --> <div id="footer"> <div class="footer-nav"> <div class="left"> <ul> <li><a href="home.html">Home</a></li> <li><a href="inspection-services.html">Services</a></li> <li><a href="payments.html">Payments</a></li> <li><a href="contact-us.html">Contact</a></li> <li class="last"><a href="home-inspector-links.html">Links</a></li> </ul> </div> </div> <div class="bottom"><p><a href="http://www.fullloop.net" class="new_window">Full Loop Inspections</a> </p> <p>Servicing <strong>COLUMBUS</strong> - Bexley - Canal Winchester - Dublin - Gahanna - Grandview Heights - Grove City - Hilliard - New Albany - Pickerington - Reynoldsburg - Upper Arlington - Westerville - Whitehall - Worthington and all surrounding cities </p> </div> </div> </div> <script type="text/javascript"> Cufon.now(); </script> </body> </html> $date = date('G:i', $row_teams['startTime']); // this date looks like this 15:30 I am trying to add 2 hours to it, any suggestions please Hey Guys. I am writing a function that will output to the user if a store is open or not.
I wrote a fucntions named is_store_open() with a variable called $day which will have the current day of the week assigned to it.
Lets say it is Saturday. it finds the case for "Saturday" and checks the opening time and closing time for Saturday.
The problem that I am facing, is when the store is open past midnight on Saturday. It then won't check the case for saturday.
Instead will check the case for Sunday. Which is not what I want, becuase it will show the store as closed when its open still open on "Saturday"
I wrote a function below that checks to see if the previous day closing hours are greater then the current time. If its true then return true and I guess that should fix the problem.
But when I wrote that it would give me an error saying that $saturday_close is undefined. I'm guessing its becuase its in a switch satement, and it does not equal to that day.
I'm a little bit confused on how to set this up. If anyone has a suggestion I would really apprecaite your help. Thanks!
function is_store_open(){ $set_time = strtotime("tomorrow"); // Lets just sat its Sunday for testing purposes $day = date("l", $set_time); $time_now = mktime("00", "00", "00"); // Now lets say the time now is 12:00am switch ($day) { case "Saturday": $open = "11:00"; $saturday_close = "1:00"; break; case "Sunday"; if($saturday_close > $time_now ){//If the $monday_close hours are greater then the current time return false the store is still open return true; } else { $open = "2:00"; $close = "22:00"; } break; } }// End of function if(is_store_open()) { echo "Oh no! There is still a glitch in the system the store needs to remain open"; } else { echo "The store is still open on Saturday"; } I have the below code wish I wish to add 12 hours to: Code: [Select] $today = date('D', mktime(0,0,0,date('m'),date('d')+0,date('Y'))); anyone to show how I add 12 hours? |