PHP - Calculating And Displaying Date
Hi, im trying to calculate and display a time elapsed from known date in db. however im having trouble displaying it. other values display no problem, just cant calculate and display date on the fly. does anyone see a problem with my query? thank you.
Code: [Select] <?php include('config.php'); $uname=$_SESSION['username']; if($_SESSION['username']){ $sql="SELECT country, nationality, passportDate FROM country WHERE uname='$uname'"; $result = mysql_query($sql) or die(mysql_error()); $passportDate _ts = strtotime($row['passportDate ']); $passportDate _str = date("M-d-Y", $passportDate _ts); $TotalTime = floor((time() - $passportDate _ts)/(60*60*24)) . ' days'; while($row=mysql_fetch_array($result)) { echo "<table border='0'> <tr> <th>Nationality</font></th> <th>Country</font></th> <th>Passport Seent Date</font></th> <th>Time Elapsed</font></th> </tr>"; echo "<tr>"; echo "<td>" . $row['nationality']."</td>"; echo "<td>" . $row['country'] . "</td>"; echo "<td>" . $row['passportDate '] . "</td>"; echo "<td>" . ('$TotalTime') . "</td>"; echo "</tr>"; } echo "</table>"; } ?> Similar TutorialsI have a table with Territory Representative Sales. It has columns that go back 26 months. The columns are SALES_OLDEST_PD_1, SALES_PD_2, SALES_PD_3, ..... SALES_PD_25, SALES_LAST_PD_26, & SALES_CURR_PD_27. SALES_CURR_PD_27 is the current month. I want to build a table with "Last Year To Date Sales" (LYTD), "Current Year To Date Sales" (CYTD), (May 2011), and (May 2010). I know that (May 2011) will be SALES_CURR_PD_27 and (May 2010) will be SALES_PD_15. The trick is that the sales year starts in November. So, I need to figure out a way to match the table columns with "Nov. 09 - May. 10 TOTAL" for (LYTD) and "Nov. 10 - May. 2011 TOTAL" for (CYTD). I am familiar with using mktime() to subtract months. But, I am not sure where to start yet on this project. Jake Code: [Select] while (odbc_fetch_row($rs)) { $M1 += odbc_result($rs,"SALES_OLDEST_PD1"); $M2 += odbc_result($rs,"SALES_PD2"); $M3 += odbc_result($rs,"SALES_PD3"); $M4 += odbc_result($rs,"SALES_PD4"); $M5 += odbc_result($rs,"SALES_PD5"); $M6 += odbc_result($rs,"SALES_PD6"); $M7 += odbc_result($rs,"SALES_PD7"); $mate += odbc_result($rs,"SALES_PD8"); $M9 += odbc_result($rs,"SALES_PD9"); $M10 += odbc_result($rs,"SALES_PD10"); $M11 += odbc_result($rs,"SALES_PD11"); $M12 += odbc_result($rs,"SALES_PD12"); $M13 += odbc_result($rs,"SALES_PD13"); $M14 += odbc_result($rs,"SALES_PD14"); $M15 += odbc_result($rs,"SALES_PD15"); $M16 += odbc_result($rs,"SALES_PD16"); $M17 += odbc_result($rs,"SALES_PD17"); $M18 += odbc_result($rs,"SALES_PD18"); $M19 += odbc_result($rs,"SALES_PD19"); $M20 += odbc_result($rs,"SALES_PD20"); $M21 += odbc_result($rs,"SALES_PD21"); $M22 += odbc_result($rs,"SALES_PD22"); $M23 += odbc_result($rs,"SALES_PD23"); $M24 += odbc_result($rs,"SALES_PD24"); $M25 += odbc_result($rs,"SALES_PD25"); $M26 += odbc_result($rs,"SALES_LAST_PD26"); $M27 += odbc_result($rs,"SALES_CURR_PD27"); } Note: By the way SMF is changing $M"8" to $M"ate". lol Hi guys! This will seem complex but I really need your help If a have a variable with a date value (i.e. $a = '23/10/2011') How do you work out the week number of that date? Thanks I've been researching online resources and seen a lot of code and functions for calculating an interval or difference between two date/time figures. What I'm trying to do is somewhat in reverse... I want to establish an interval to determine a past date/time to be used in a MySQL Query but just can't figure the critical part.
// STEP #1 = DEFINE THE DESIRED DATE/TIME DIFFERENCE INTERVAL DESIRED FOR QUERIES $timeinterval = '60'; // Interval is in MINUTES and can be changed as desired // STEP #2 = GET THE CURRENT LOCAL SERVER DATE/TIME // as YYYY-MM-DD HH:MN:SE $serverdatetime = date('Y-m-d H:i:s'); // My local Server Date & time // STEP #3 = CALCULATE THE ***PAST** (OLDER) DATE/TIME LIMIT BASED ON $timeinterval (Minutes) // as YYY-MM-DD HH:MN:SE $pastdatetime = [stuckinarut here] <- 60 MINUTES prior to current Server Date/Time // STEP #4 = MySQL QUERY (`submitted` column is auto-timestampped on record insertions) $sql="[columnsblahblah] FROM mydb WHERE `submitted` >= $pastdatetime";In a Perfect World, the MySQL Query would yield ONLY records with a `submitted` DATE/TIME equal to 60 Minutes (or less) PRIOR TO from the Current Server Time. Any assistance is appreciated! Thanks. -FreakingOUT I was wondering if someone could help me out with calculating and displaing dates. Below i posted html of my page and also php for it. It has a field called "DATE1" thats where visitors input the date, after that it gets posted to db and i pull it and disply it in a grid on my site. I would also like to have a column that shows time elapsed between the "DATE1" and current date. I sorta figured how to calculate the dates using the php, but really stuck on how do i actually display it now. from what i been told so far i shouldn't create a new column for that, instead calculate and display the values on the fly, could some step me through this please, or get me started... thanks so much! my html Code: [Select] <form id="signupForm" method="POST" action="processform.php"> <div> <fieldset> <legend>Signup Form</legend> <label for="Country">*Country:</label> <select name="Country"> <option value="">-----</option> <option value="Canada">Canada</option> <option value="UK">UK</option> <option value="France">France</option></select></div></p> <p><label for="Nationality">*Nationality:</label> <div><select name="Nationality"> <option value="">-----</option> <option value="Afghanistan">Afghanistan</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option></select></div></p> <p><label for="Province">*Province:</label> <div><select name="Province"> <option value="">-----</option> <option value="British Columbia">British Columbia</option> <option value="Alberta">Alberta</option> <option value="Saskatchewan">Saskatchewan</option></select></div></p> <p><label for="DATE1">*Date:</label> <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option> <option value="1">01</option> <option value="2">02</option> <option value="3">03</option></select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option></select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option> <option value="2014">2014</option> <option value="2013">2013</option> <option value="2012">2012</option></select> </fieldset> </div> <p> <div align="center"><input type="submit" name="submit" value="Submit your entry"></div> </p> </form> <p> Code: [Select] <?PHP $hostname = "localhost"; $db_user = ""; $db_password = ""; $database = ""; $db_table = ""; $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); //here i use this to calculate the date but really unsure how to further display the results Code: [Select] if (isset($_REQUEST['submit'])) { // Get current time, or input time like in $date2 $date1 = time(); // Get the timestamp of DATE1 $date2 = mktime(0,0,0,date($_POST['month']),date($_POST['day']),date($_POST['year'])); $dateDiff = $date1 - $date2; $TotalTime = floor($dateDiff/(60*60*24)); echo "$TotalTime"; } for posting to db Code: [Select] if(isSet($_POST['submit'])) { $country = mysql_real_escape_string($_POST['Country']); $nationality = mysql_real_escape_string($_POST['Nationality']); $province = mysql_real_escape_string($_POST['Province']); $date = $_POST['day'].'-'.$_POST['month'].'-'.$_POST['year']; $myQuery = "INSERT INTO {$db_table} (`Country`,`Nationality`,`Province`,`DATE1`) VALUES ('{$country}','{$nationality}','{$province}','{$date}')"); if(mysql_query($myQuery)) { echo 'Record inserted.'; } else { echo 'An error has occurred: '.mysql_error(); } } ?> Hi, everyone. I am trying to put together a WordPress for a client and although I hate asking for help - I am at a total standstill! She wants date tabs on the left of each post (on the main homepage and then each post; NOT pages- obviously, since those aren't 'dated', per say...). Here is the function.php code I am using: Code: [Select] // custom date tab - creates two divs // first div pulls in a background png image named by date // and adds year dynamically. Placed via css to left of post as // a hanging tab. remove_action('genesis_before_post_content', 'genesis_post_info'); add_action('genesis_before_post_content', 'family_tree_post_info'); function family_tree_post_info() { if(is_page()) return; // don't do post-info on pages echo '<div class="post-date-wrap">'; echo '<div class="post-date post-date-background" style="background:url('; echo CHILD_URL;; echo '/images/date'; echo the_time('F'); echo '/'; echo the_time('M'); echo '.png) no-repeat 4px -3px">'; echo the_time('Y'); echo '</div>'; echo '</div>'; echo '<div class="post-info">'; genesis_post_comments_link(__('Leave a Comment', 'child'), __('1 Comment', 'child'), __('% Comments', 'child')); edit_post_link(__('(Edit)', 'child'), '', ''); // if logged in echo '</div>'; } This shouldn't matter [much] - but here is the style.css for the post-dat: Code: [Select] .post-date-wrap { width: 65px; height: 73px; } .content-sidebar .post-date-wrap { float: left; margin-top: -42px; margin-left: -107px; background: url(images/dates/bgd-left.png) no-repeat top left; } .content-sidebar-sidebar .post-date-wrap { float: left; margin-top: -42px; margin-left: -84px; background: url(images/dates/bgd-left.png) no-repeat top left; } .full-width-content .post-date-wrap { float: left; margin-top: -42px; margin-left: -89px; background: url(images/dates/bgd-left.png) no-repeat top left; } .sidebar-content .post-date-wrap { width: 65px; height: 73px; float: right; margin-top: -42px; margin-right: -89px; background: url(images/dates/bgd-right.png) no-repeat top left; } .sidebar-sidebar-content .post-date-wrap { width: 65px; height: 73px; float: right; margin-top: -42px; margin-right: -86px; background: url(images/dates/bgd-right.png) no-repeat top left; } .sidebar-content-sidebar .post-date-wrap, .sidebar-content-sidebar .post-date { visibility: collapse; height: 0; width: 0; } .post-date { width: 62px; height: 17px; display: inline; text-align: center; color: #4e260d; padding: 50px 0 0 5px; font-size: 9px; } .content-sidebar .post-date, .content-sidebar-sidebar .post-date, .full-width-content .post-date { float: left; } .sidebar-content .post-date, .sidebar-sidebar-content .post-date { float: right; } Here are two pictures: * I have one of these for every day; for each month - found in images/dates - thereafter organized by months (in written out for October, November; then 1.png; 2.png, etc. for each DAY of the month) This is how it is coming up right now; only the year (that really small lettering that says 2011): I need the images with the month & date to appear inside of that box/tab. I have been working on this for too long and cannot get it to go. My 'thing' is design, so I really hope you can all help me! I just can't get PHP down.. I am sure it is something so ridiculous that I cannot see. Thanks for your help!! - Nicole Okay, I have a field called "created" in my `users` table, and I'd like to be able to view it in-game on an administrator panel. However, it's being captured in a long number format (i.e. 1335076320) which, I believe, has to deal with seconds and such. How can I take that number and convert it to an actual date/time output? Here's how I currently have it coded in my php form: <tr><td width="20%">Registered On:</td><td>{{created}}</td></tr> So, obviously it's not converting it to any format, rather just pulling that 1335076320 number out. Thanks in advance! I am writing code in the ZEND framework and i need to get the current day in like 2012-02-03: In the controller: Code: [Select] $time=date('YYYY-mm-dd',time()); $this->view->time=$time;In view: Code: [Select] <?php echo $this->time; ?> Output: 2012201220122012-0202-0303 I dont know why it is displaying like that instead of once...there is no loop anywhere. Hey, I'm using a script which allows you to click on a calendar to select the date to submit to the database. The date is submitted like this: 2014-02-08 Is there a really simple way to prevent rows showing if the date is in the past? Something like this: if($currentdate < 2014-02-08 || $currentdate == 2014-02-08) { } Thanks very much, Jack I have a timestamp field in my mySQL table and need to check if it's 14 day or older. What's the best way to do this? Any help will be appreciated Hi everyone, I'm trying to add up the current time with a specified amount of seconds and insert it in a database afterwards. When I add say: 500 seconds to the current time it adds a whole lot more and I get a number few thousands higher than I want. Can anyone help me???. Gr Ryflex I have a form that I will post the code for. In my form I have a few things that need to be filled out.... Age, Gender, Coverage Amount, and Health Conditions. The Health Conditions are 5 check boxes the user can select. Now what I'm trying to do is make it so that when the user hits submit the Age and number of Health Conditions checked off will do this... if # of Health Conditions <=2 and Age is <= 30 then = $0.90 if # of Health Conditions =>2 but <=5 and Age is <= 30 then = $1.20 Then after those are calculated I need these to apply to the code... - Men are charged a 5% gender risk - Students receive an 8% discount Here is a example of what I'm trying to get this to do... If you are a 23 year old male student who smokes but has no other risk factors your premium amount per $10,000 would be $0.90 * 1.05 * 0.92 = $0.8694. If you wanted to take out a $150,000 life insurance, your monthly premium would be $13.04 (15 * $0.8694). I'm doing a life insurance form and these are some of the calculations that need to go into it. I'm not sure how I would code these. If someone could help me get started that would be great. Here is my form code and the display page. Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Lab 4</title> </head> <body> <?php ?> <form method="POST" action="lab4form.php"> <label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name" /><br /> <label for="last_name">Last Name:</label> <input type="text" id="last_name" name="last_name" /><br /> <label for="coverage_amount">Coverage Amount:</label> <input type="text" id="coverage_amount" name="coverage_amount" /><br /> <label for="gender">Gender:</label> <br> <input id="male" type="radio" value="Male" name="gender" /> <label for="male">Male</label> <input id="female" type="radio" value="Female" name="gender" /> <label for="female">Female</label> <br /> <br> <label for="Age">Age:</label> <input type="text" id="age" name="age" /><br /> <br> <label for="health">Health Conditions</label> <br /> <input type="checkbox" name="health" value="Heart Disease" /> <label for="health">Heart Disease</label><br /> <input type="checkbox" name="health" value="Diabetes" /> <label for="health">Diabetes</label><br /> <input type="checkbox" name="health" value="High Blood Pressure" /> <label for="health">High Blood Pressure</label><br /> <input type="checkbox" name="health" value="Smoker" /> <label for="health">Smoker</label><br /> <input type="checkbox" name="health" value="Lung Disease" /> <label for="health">Lung disease</label><br /> <FORM METHOD=POST ACTION="lab4form.php"> <P>Employment Status<BR> <SELECT NAME="employment"> <OPTION VALUE="Unemployed">Unemployed <OPTION VALUE="Full-Time">Full-Time <OPTION VALUE="Part-Time">Part-Time <OPTION VALUE="Student">Student </SELECT><br /> <br> <label for="comments">Comments</label> <input type="text" id="comments" name="comments"/><br /> <br> <input type="submit" value="Add User" name="btn_add" /> </form> </body> </html> Display Page Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Lab4</title> </head> <body> <div id="page"> <div id="main"> <div id="content"> <h2>Life Insurance</h2> <table> <tr> <th>Coverage Amount</th> <th>Name</th> <th>Gender</th> <th>Age</th> <th>Health Conditions</th> <th>Employment Status</th> <th>Comments</th> </tr> <?php $coverage_amount = $_POST['coverage_amount']; $age = $_POST['age']; $health = $_POST['health']; if $age = ?> <tr> <td><?php echo $_POST['coverage_amount'];?></td> <td><?php echo $_POST['first_name'];?></td> <td><?php echo $_POST['gender'];?></td> <td><?php echo $age?></td> <td class="right"><?php echo $_POST['health'];?></td> <td class="right"><?php echo $_POST['employment'];?></td> <td><?php echo $_POST['comments'];?></td> </tr> </table> </div> </div> <div id="footer"></div> </div> </body> </html> Not asking someone to do this for me. Just looking for someone to help me get going, and show me how to do this. Need helping calculating text boxes on a form. I want my form to display the premium of $10,000 health insurance. What I mean by this is that I have a form that involves filling out life insurance information. This is the table I'm basing this off of... Age <=30 #health conditions <=2 $0.90 2 <= #health conditions <=5 $1.20 The number of health conditions is equivalent to the number of check boxes that were checked. Additionally, the following conditions apply: - Men are charged a 5% gender risk - Students receive an 8% discount For example, if you are a 23 year old male student who smokes but has no other risk factors your premium amount per $10,000 would be $0.90 * 1.05 * 0.92 = $0.8694. If you wanted to take out a $150,000 life insurance, your monthly premium would be $13.04 (15 * $0.8694). I'm new to php and haven't really calculated forms before... here is my form codes.... Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Lab 4</title> </head> <body> <?php ?> <form method="POST" action="lab4form.php"> <label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name" /><br /> <label for="last_name">Last Name:</label> <input type="text" id="last_name" name="last_name" /><br /> <label for="coverage_amount">Coverage Amount:</label> <input type="text" id="coverage_amount" name="coverage_amount" /><br /> <label for="gender">Gender:</label> <br> <input id="male" type="radio" value="Male" name="gender" /> <label for="male">Male</label> <input id="female" type="radio" value="Female" name="gender" /> <label for="female">Female</label> <br /> <br> <label for="Age">Age:</label> <input type="text" id="age" name="age" /><br /> <br> <label for="health">Health Conditions</label> <br /> <input type="checkbox" name="health" value="Heart Disease" /> <label for="health">Heart Disease</label><br /> <input type="checkbox" name="health" value="Diabetes" /> <label for="health">Diabetes</label><br /> <input type="checkbox" name="health" value="High Blood Pressure" /> <label for="health">High Blood Pressure</label><br /> <input type="checkbox" name="health" value="Smoker" /> <label for="health">Smoker</label><br /> <input type="checkbox" name="health" value="Lung Disease" /> <label for="health">Lung disease</label><br /> <FORM METHOD=POST ACTION="lab4form.php"> <P>Employment Status<BR> <SELECT NAME="employment"> <OPTION VALUE="Unemployed">Unemployed <OPTION VALUE="Full-Time">Full-Time <OPTION VALUE="Part-Time">Part-Time <OPTION VALUE="Student">Student </SELECT><br /> <br> <label for="comments">Comments</label> <input type="text" id="comments" name="comments"/><br /> <br> <input type="submit" value="Add User" name="btn_add" /> </form> </body> </html> hey guys just a quick onei having a blank moment i have an array at themoment pulling from a database. i basically want to split this array based on a min and max value. divide it equally and then give it a value on each smaller array and where each array starts... to make it clearer - say i have an array with 600 values in i want it split up into 8 smaller arrays link so Array ( => Array ( [start] => 0 [split] => 75 ) [1] => Array ( [start] => 75 [split] => 75 ) [2] => Array ( [start] => 150 [split] => 75 ) carrying on till the end ) how would i do this. Hello, I am having a heck of a time trying to get time right on my website lol When a user logs in, they select what timezone they are in (UTC -1,-2,-3,-4,-5,-6....) Now this is great an all, but with daylight savings time my times are off by one hour in either direction. How do sites like this get times to work for users so easily? Thanks! weee Hello Please take a look at attatchment: Lets say for example I wanted to check the streak of clan1 (column) = 9 (rows) The streak would be 5 because each row has score1 greater than score2. So I want to calculate the streak of the value in clan1 column that has score1 greater than score2 in rows, if score2 is greater than score1 in a row then the streak breaks if that makes sense? let me know if I need to re-phrase. Secondly, if there are two streaks, I want it to calculate the highest one. Thanks for any help !! Hi, This query will show match stats of a user, the query is the output in the attatchment. I want to calculate the streak if score1 is greater than score2. So basically, in the attatchment I have selected 3 rows, these 3 rows should be a 3 streak because the user won 3 matches in a row (according to date) Code: [Select] SELECT * FROM webs_cup_matches WHERE ladID='17' AND (clan1 != '0' AND clan2 != '0') AND (clan1='2630' || clan2='2630') AND (clan1 != '2147483647' AND clan2 != '2147483647') ORDER BY date DESC any help appreciated. Okay guys, I figured out how to calculate monthly subscriptions into days, but I am stuck on calculating multiple monthly subscriptions.. for example Heres my mysql table: How would I calculate this: Subscription Start: February 21, 2011, 12:00 am Subscription End: April 22, 2011, 12:00 am Subscription Left: 60 days My trouble is that when the user buys another month, it calculates the end time based on the new "Subscription Start Date". Or vice versa. Any ideas guys? Thanks. I have this block of code that was written by someone years ago :-
<?php class BoDelivery { public function EstimatedDays($off, $standard_days, $saturday, $delay) { $today = date("N"); // Weekday - number 1-7 $now = strtotime("now"); // Unix $off_array = explode(":", str_replace(".", ":", $off)); $off_unix = mktime($off_array[0], $off_array[1], "00", date("n"), date("j"), date("Y")); $sending_days_from_now = 0; if ($now > $off_unix) { $sending_days_from_now++; } $sending_day = $today + $sending_days_from_now; switch ($sending_day) { case 6: $sending_days_from_now++; $sending_days_from_now++; break; case 7: $sending_days_from_now++; break; } $sending_day = $today + $sending_days_from_now; // Estimated delivery time $delivery_days = $standard_days; $over_weekends = 0; // Add Delay if ($delay) { $delivery_days = $delivery_days + $delay; } if ($sending_day == 5 && !$saturday) { $delivery_days++; $delivery_days++; $over_weekends++; } $delivery_day = $sending_day + $delivery_days; switch ($delivery_day) { case 6: if (!$saturday) { $delivery_days++; $delivery_days++; $over_weekends++; } break; case 7: $delivery_days++; $over_weekends++; break; } if ($over_weekends == 0 && $delivery_days > 5) { $delivery_days++; $delivery_days++; } $delivery_day = $sending_day + $delivery_days; switch ($delivery_day) { case 13: $delivery_days++; $delivery_days++; $over_weekends++; break; case 14: $delivery_days++; $over_weekends++; break; } $delivery_day = $sending_day + $delivery_days; $delivery_days = $delivery_day - $today; return $delivery_days; } } ?>
Which is supposed to calculate the number of days for delivery, taking into account weekends & with a delay variable that we can set. It isn't working as expected, may never have worked(??) or maybe down to PHP upgrades. For example, if today (18th)I set the delay to 2 days the delivery estimate is the 21st (which is correct), if I change the delay to 3 OR 4 it becomes the 24th, 5 then becomes the 26th! I can't get my head around the code at all, I wonder if someone could assist in what may be going on or add comments to the code snippets so I can maybe work it out?
Thanks for any help. hi all, i'm trying to calculate the time difference between two dates in hours, for example: 2010-12-13 15:26:56 and 2011-12-13 15:26:56 i tried mktime but wasn't able to get a working function. Thanks for the help |