PHP - Adding Time Values And Displaying In Correct Order
Hi,
I'm inserting hours and minutes per user into a database where they have their own fields. (userid, hours, mins) I've a small issue when displaying the data. When I run my query I sum the total hours and minutes per user which results in data such as the following userid1 - 2 hours 15 mins userid2 - 1 hour 100 mins The query orders by hours and then mins desc When I'm displaying the data (as I'm looping through the results array) I perform a calculation to convert the mins to hours so it now reads userid1 - 2 hours 15 mins userid2 - 2 hours 40 mins so the webpage displays userid1 first when i want userid2 to be the first record displayed (Hours desc) Can anyone recommend a solution to this ? Will I need to create another table and update it as hours and minutes are being entered and display results from that table instead ? Can I order the data after I carry out the mins to hours calculation ? many thanks in advance for any suggestions.... tmfl Similar TutorialsWhen I echo the current time, the hour is what it is plus one: echo date("h:i:s:A"); Real time is 3:46:33 Would display 4:46:33 What is wrong here? PHP date and time function is not showing correct time on my local system I have the following php code date_default_timezone_set("Africa/Lagos"); $date = date('d-m-y h:i:s'); echo "Server Time ".$date ."<br>"; echo "The time is " . date("h:i:sa")."<br>"; $current_datetime = date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa'))); echo "Current time1: ".$current_datetime . "<br>";
Output
Server Time 21-05-21 09:55:39
Expected Output
Server Time 21-05-21 10:55:39
Any help would be appreciated. Edited May 21 by Ponel Yesterday I had this problem - I created this website in wordpress www.porthopehealthcentre.com It displayed correct in firefox but not ie. There was an extra closing </div> tag in my header php. When I deleted it, it displayed correct in ie but not firefox. So I added the below tag to my header.php file. It now displays correct in all browsers except google chrome, latest version. What can I do to fix this so it displays correct in google chrome - anybody? Thanks in advance - I really appreciate it. <!--[if IE]> <![endif]--> <!--[if !IE]><!--> </div> <!--<![endif]--> I have a script that is working apart from I can only get json to send 1 result instead of multiple. The values are being correctly processed and inserted into the db, apart from the echo $json. For example, a user will input 3 input elements in a form. item1.item2,item3, Jquery will serialize and then send to the php page using $.ajax. I have created a foreach loop to handle the post, but I can only get 1 result in the echo $json. I would be grateful if someone check my code and show me where I am going wrong?. Many thanks Code: [Select] <?php session_start(); $new = 1; $activity = 'Box Retrieval'; $mobile = 'Submitted from mobile'; $company = $_SESSION['idcode']; $authorised = mysql_real_escape_string($_POST['BRV_brtrvrb']); $service = mysql_real_escape_string($_POST['BRV-id-service-type']); $department = mysql_real_escape_string($_POST['BRV-brtrv-department']); $address = mysql_real_escape_string($_POST['BRV-brtrv-address']); $boxcount = mysql_real_escape_string($_POST['BRV-brtrv-slider']); foreach ($_POST['BRVbrtrv_boxnumber'] as $box) { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); header("Content-type: application/json"); $json = ""; if(empty($service)) { $json .= "{\"ErrorService\": \"ERROR: You mest select a service level\"}"; } else if($department=="Choose Department") { $json .= "{\"ErrorService\": \"ERROR: You must select a department\"}"; } else if($address=="Choose Address") { $json .= "{\"ErrorService\": \"ERROR: You must select a retrieval address\"}"; } else if(empty($box)) { $json .= "{\"ErrorService\": \"ERROR: You must enter a box for retrieval\"}"; } else { $json .= "{\n"; $json .= "\"boxnumber\": \"".$box."\",\n"; $json .= "\"boxcount\": \"".$boxcount."\"\n"; $json .= "}\n"; $query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `notes`, `new`) VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($box).'\', NULL, NOW(), \''.$mobile.'\', \''.$new.'\');'; mysql_query($query) or die('Error, query failed'); } } echo $json; ?> Hello, I got myself a RSS feed for my posts, which can be seen he codexplained.tk/feed/rss.php This is the code used for it: Code: [Select] <?php // header header("Content-Type: application/xml; charset=UTF-8"); require("../widgets/functions.php"); // Make a MySQL Connection connectToDB(); echo ' <rss version="2.0"> <channel> <title>Codexplained RSS Feed</title> <link>http://www.codexplained.tk</link> <description>For all your coding needs</description> <language>en</language> '; $query = "SELECT * FROM posts ORDER BY Id DESC LIMIT 15"; $res = mysql_query($query); $num=mysql_numrows($res); $i = 0; while ($i < $num) { $subject=mysql_result($res,$i,"Subject"); $id=mysql_result($res,$i,"Id"); $content=mysql_result($res,$i,"Content"); $content = str_replace("<br />", "\n", $content); $content = str_replace("<?php", "<?php", $content); $content = str_replace("?>", "?>", $content); $content = htmlentities($content); $content = substr($content, 0,400); echo ' <item> <title>'.$subject.'</title> <link>http://www.codexplained.tk/index.php?postDetails='.$id.'</link> <description>'.$content.'</description> </item>'; $i++; } echo ' </channel> </rss> '; ?> Now, I wish to know how to make the time display correctly, and not just according to refresh date. I tried something with <pubDate> but I couldn't figure it out. Help would be appreciated! Hi, I got this code which is meant to display the login details of the person that is logged in, however it just displays the details of the last person in the mysql table. I have set up some test logins, so if I login as paul1 the details for paul3 are displayed...confused Anyway, here is the page which displays the details Code: [Select] <?php session_start(); // This checks to make sure the session variable is registered // WARNING: DO NOT DELETE THE FOLLOWING LINE OF TEXT if( isset($_SESSION['username']) && isset($_SESSION['sid'])) { // You are free to edit the following code to suit your requirements include_once("../../data/server.php"); include_once("../../lib/userdata.php"); // THIS BIT WORKS AND DISPLAYS THE USERNAME $data = mysql_query("SELECT * FROM members") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { include("../../lib/userinfo.php"); //////////////////////////////////////////// WARNING: YOU SHOULD NOT EDIT ANYTHING ABOVE THIS LINE //////////////////////////////////////////////////////// ECHO <<<PAGE <!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> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>$siteName</title> <link rel="stylesheet" href="../../userstylesheet.css" type="text/css" /> </head> <div id="page"> <img alt="" src="../../images/leftCurve" height="6" width="6" id="left" /> <img alt="" src="../../images/rightCurve.gif" height="6" width="6" id="right" /> <div id="pageName"> <h1>$siteName</h1> </div> <div id="pageNav"> <div id="sectionLinks"> <a href="profile.php?username=$username">My Profile</a> <a href="modify.php?username=$username">Personal Details</a> <a href="message.php?username=$username">Messages</a> <a href="../../logout.php?username=$username">Logout</a></div> </div> <div id="content"> <div class="feature"> <h2>Welcome $username </h2> <p>This is the demonstration home.html template. You are free to edit this or any of the other templates to suit your own needs. </p> <p>This is the first page your member will see once they have logged in. </p> <p>If you look at the code for this page, you will see that all HTML code is placed between the ***PAGE and PAGE; tags. Please note that the three * should be replaced with the < character. This format must be kept to ensure that the user variables work. Changing this format may result in errors being returned.</p> <p>You may call member information using the $ tag and the variable name eg $ firstname without the space, will show the members first name, such as $firstname</p> <p>For any information please visit our site http://www.membersitemaker.co.uk. User guides will be added shortly and the forum will soon be full of help. </p> </div> </div> <div id="information"> <a href="#">About Us</a> | <a href="#">Site Map</a> | <a href="#">Privacy Policy</a> | <a href="#">Contact Us</a> | ©2011 $siteName </div> </div> </body> </html> PAGE; } //////////////////////////////////////// WARNING: DO NOT DELETE ANYTHING BELOW THIS LINE ////////////////////////////////////////////////////////// } else { // This will redirect the user to the login page if the session variables do not exist header( "Location: ../../../login.html" ); } ?> And here is the code for userdata.php Code: [Select] <?php // Decode sitename function decode_variable(&$siteName) { $siteName = urldecode($siteName); $siteName = str_replace('%20',' ',$siteName); return $siteName; } decode_variable($siteName); // Connnect to MySQL database include_once("../../data/mysql.php"); $mysqlPassword = (base64_decode($mysqlpword)); $db = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die ("Error connecting to database"); mysql_select_db("$dbname", $db) or die ("An error occured when connecting to database"); // Carry out MySQL query ?> and userinfo.php Code: [Select] <?php $username = $info['username']; $firstname = $info['firstname']; $lastname = $info['lastname']; $address = $info['address']; $town = $info['town']; $county = $info['county']; $postcode = $info['postcode']; $email = $info['email']; $birth_year = $info['birth_year']; $country = $info['country']; $telephone_number = $info['telephone_number']; $mobile_number = $info['mobile_number']; $nickname = $info['nickname']; As always, your help is much appreciated Paul Well this is pretty much the first time I'm attempting something new without a tutorial aiding me ( I know your gonna think I probably should have used 1 when checking my code ) Honestly my brain is fried, but I have a deadline for tomorrow. Basicly I'm sending an email upon registration ( email sends fine ) I made it so the email display the students name and course. However in my registration a student can select contact y or n, which determines whether the student wants to be contacted by other students. So when sending an email to a student who selected n for contact, is should only display the students name and course (sname, fname, cname). However, for a student who selected y for contact, it should display the name and course aswell of a list display the sname,fname and email of all the other students in my student table who selected y in their contact_flag field. Here is my misguided code: Code: [Select] <?php function sendmail(){ $cname = mysql_real_escape_string($_POST['cname']); $sname = mysql_real_escape_string($_POST['sname']); $fname = mysql_real_escape_string($_POST['fname']); $contact = mysql_real_escape_string($_POST['contact']); $Name = "Student Course Registration"; //senders name $email = "goldie@telkomsa.net"; //senders e-mail adress $recipient = ($_POST['email']); //recipient $mail_body = "Congratulations $fname $sname. You have successfully registered for the following course: $cname "; //mail body $subject = "Course registration successful!"; //subject $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields if ($contact=='y'){ $query = "SELECT sname,fname,email FROM student WHERE $contact = ['contact_flag'] "; $run = mysql_query($query) or die(mysql_error()); $found = mysql_fetch_array($run); while ($found = mysql_fetch_array($run)) $contactemail=$person['email']; $contactsname=$person['sname']; $contactfname=$person['fname']; $mail_body2 = "Congratulations $fname $sname. You have successfully registered for the following course: $cname. Here is a list of all the students who you may be in contact with: $contactsname, $contactfname, $contactemail"; //mail body for contact flag { mail($recipient, $subject, $mail_body2, $header); } } else { mail($recipient, $subject, $mail_body, $header); } } ?> I am not receiving any errors from it, and I'm receiving an email which displays $mail_body instead of $mail_body2 which is my else statement. Please, help would be appreciate. Thanks in advance. Hi all, I am working on my PHP script to set up the date with the time for the autoresponder so I can send out the emails at the specific time. I need some help with set up the correct day date with the time, because on my code when I have two different times `06:00` and `20:00`, as both of them will show the time with the current day date, e.g: 28-11-2019. I find that my code have set up the date as incorrect because the time I have `06:00` which it should have set up with the next day date, e.g 29-11-2019 instead of 28-11-2019 and the time `20:00` should set up with the current day date as my current time is `15:26pm` right now.
2019-11-25 06:00
06:00
$auto_responders = $link->prepare('SELECT * FROM autoresponder WHERE campaign = ? ORDER BY id ASC'); $auto_responders->execute([$campaign]); $auto_responders->setFetchMode(PDO::FETCH_ASSOC); $auto_responders = $auto_responders->fetch(PDO::FETCH_ASSOC); $get_time = $auto_responders['send_time']; if ($get_time >= strtotime('00:00')) { $autoresponder_date = date('Y-m-d', strtotime($get_time . ' +1 day')); } else { $autoresponder_date = date('Y-m-d', strtotime($get_time)); } $send_time = $autoresponder_date . ' '. date('H:i ', strtotime($get_time));
Can you please show me an example how I can set up the day date for the time `06:00` and `20:00` as if I have the time is `06:00` then check if the time have passed before I could do anything to send the email and it is the same for the time `20:00:00`? Thank you.
My DB is holding time as "11:00 AM" "10:00 AM" ect... Is there a way to convert these to time() after I have queried the DB for results? <?php connect(); $query = "SELECT * FROM party WHERE year(DOP) = '2011' order by PTIMES asc"; $qt=mysql_query($query); echo mysql_error(); while($nt=mysql_fetch_array($qt)){ echo " <tr height='30'> <td id='label'>".$nt['NOC']."</td> <td id='labeloff'>".$nt['CFN']." ".$nt['CLN']."</td> <td id='labeloff'>".$nt['type']."</td> <td id='label'>".birthday($nt['DOB'])."</td> <td id='label'>$DOP</td> <td id='labeloff'>".$nt['PTIME']."</td> <td id='labeloff'>".$nt['act1']."</td> <td id='labeloff'>".$nt['act2']."</td> <td id='label'>".$nt['pizza']."</td> <td id='label'>".$nt['fc']."</td> </tr> "; }free($qt); ?> Thank you. I have a table in my database which stores lot of file names & file path. iam currently listing the files on the basis of its uploaded date (stored in databse) but I want to list it on the basis of its file modification time. How i can do it ?? my current sql statement is Code: [Select] $sql = mysql_query("SELECT * FROM `files` WHERE `cid` = '$types' ORDER BY `uploaded` DESC LIMIT $start,$limit"); anyone can help me ?? Hello.. I'm a somewhat beginner in php, but i decided to create my own basic forums system with a content managing system. Right now I'm trying to figure out how to make a thread link be at the top of the list for the forum display page, when someone posts a response or creates a new thread. HELP MUCH APPRECIATED What would be the correct way to use returned sql results as variables. I have tried the way in my script, but Im unsure of how I can echo just one of the $nt variables further on in my script. $code = $_GET['postcode']; $shortcode = substr($code,0,2); $query =mysql_query ("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3"); echo mysql_error(); echo "<p>The email addresses you have requested are;</p>"; while($nt=mysql_fetch_row($query)){echo "$nt[0],$nt[1],$nt[2]<br>";} echo "<p>please use $nt[2] if you ever want to contact me</p>"; //this line doesnt work for me ?></body></html> is there is a way to assign variables such as; $nt[0] = $firstemail $nt[1] = $secondemail $nt[2] = $thirdemail this way I could use each of my sql values, just as I would any other variable? Hi, I'm not necessarily looking for code examples for my problem, (they are welcome but its not my explicit goal). I'm looking more for advice on how to go about tackling the problem i have. what i already have Currently i have a search getting results from a MySQL database. The results are ordered by one field that signifies their importance. Many of these results have the same value in this one column, and i have a switch statement to distinguish different content/styles for the different values. (there are just over 10 different bands) So the most important results are shown first and with a different style to the ones below it. (some bands have the same styling) what i need I would like to be able to randomize the results but only randomize the results within their specified bands. e.g. lets say theres 100 results in band A and 100 in band B. I still want everything in band A to be listed above band B, but i would like to randomly change the order of the results in band A and band B, (in this case once a week). I need to save the order of this randomization (client/server side?) for a week, then run the script again to set a new order. Now I would still class myself as an amateur php'er so at this stage i dont even know how i would go about separating the bands and randomizing their results independantly, let alone causing this functionality to run only once a week. So can anyone suggest a way i might go about this? I'm pretty stuck right now. Cheers hi guys I have two functions and they work well, but now i have a problem in the last if the email is sent like www.yoursite.com/reset_password.php?userid=0&code= and not http://your.url/set_new_password.php?userid=564979&code=54c4a2767c2f485185ab72cdcf03ab59 so, the problem is that i can get the value of hash and the userid. If i do an echo "$hash" in the function code(); it shows the value like 564979&code=54c4a2767c2f485185ab72cdcf03ab59 my question is, how i can do the same in the last if? above two functions. Same occur in the last select select userid from password_reset where code=? none userid is showed, it is always zero. Code: [Select] <? function check($sql, $db, $email) { if(!empty($_POST['email'])) { $email = $_POST["email"]; if ($sql = $db->prepare("select email from users where email=?")) { $sql->bind_param('s', $email); $sql->execute(); $sql->bind_result($email); if ($sql->fetch()) { return true; } else { return false; } } } } function code($sql, $db, $hash, $pwdHasher, $userExists, $sendPass) { if (check($sql, $db, $email)) { $pwdHasher = new PasswordHash(8, FALSE); $hash = $pwdHasher->HashPassword($userExists["email"]); $sendPass=$hash; ($sql = $db->prepare('insert into password_reset (code) values (?)')); $sql->bind_param('s', $hash); $sql->execute(); $sql->fetch(); return true; } } if (code($sql, $db, $hash, $pwdHasher, $userExists, $sendPass)) { ($sql = $db->prepare("select userid from password_reset where code=?")); $sql->bind_param('s', $hash); $sql->execute(); $sql->bind_result($hash); $sql->fetch(); echo $hash; $pwrurl = "www.yoursite.com/reset_password.php?userid=" .$hash . "&code=" . $sendPass; $mailbody = "Dear user,<br><br>If this e-mail does not apply to you please ignore it. It appears that you have requested a password reset at our website www.yoursitehere.com<br> To reset your password, please click the link below. If you cannot click it, please paste it into your web browser's address bar.<br> <a href='$pwrurl'>$pwrurl</a> <br> <br> Thanks,\nThe Administration"; $mail->MsgHTML($mailbody); $mail->AddAddress($email,"Membro"); $mail->IsHTML(true); if(!$mail->Send()) { echo "Deu erro: " . $mail->ErrorInfo; } else { echo "Enviado com sucesso"; } $sql->close(); $db->close(); } ?> any help? thanks I am trying to display images on a html page alphabetically in descending order by their filename. <?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a><br />;"; } } closedir($handle); } ?> Any help is greatly appreciated! Hi again guys, Right, I have two times being output from a database... The first time is the time something is booked in for (eg: 16:30:00) and the second time is the duration that thing is booked in for (eg an hour and a half - 01:30:00) Now, I want to figure out when that thing will be finished, so using the two examples above, it will be finished at 18:00:00, I've tried a few things with no success Please help Cheers I used the following code to convert the length in seconds to min:second format $sec=$duration % 60; if ($sec=="0"){ $min=$duration/60; $duration=''.$min.':00'; }else{ $min=(($duration-$sec)/60); $duration=''.$min.':'.$sec.''; } everything is OK, but it shows 35:5 instead of 35:05. I mean it disregards the first 0 when the second is 1-digit.
I have a given date on a webpage, that I'm scraping to insert into a DB. The date is in this format: $date = 'Sun, Feb 9<br />3:00 PM ET': $healthy = array("<br />", " ET", "Sun.", "Mon.", "Tue.", "Wed.", "Thu", "Fri.", "Sat.", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); $yummy = array(" ", "2020", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $date = str_replace($healthy, $yummy, $date1); $start = date('Y-m-d H:i:s', strtotime("$date -120 minutes"));
I'm trying to add some minutes ($interval) to any given time, but my results aren't as expected... Code: [Select] <?php $year="2012"; $opentime="09"; $interval="10"; $thedate = "$year:01:01 $opentime:00:00"; $startdate = strtotime($thedate); $date = date("Y-m-d",$startdate); $time = date("g:i a",$startdate); $newtime = strtotime("+$interval", $time); $nexttime = date("g:i a",$newtime); echo $date; echo "<br>"; echo $time; echo "<br>"; echo $nexttime; is returning: 2012-01-01 9:00 am 7:10 pm I was expecting the last to be 9:10 am. Can someone point me in the right direction? Thanks. Hey! How've you been? This should be simple but I can't make it work. I have a few database values from a user entry. The page queries the database and I can print them no problem. I want to add them and display the total of these values. I've tried about two dozen ways of doing and I just can't get them to add. Where I have "fee_total" I want it to be the sum of the values "fee+upgrade7+tax". This is the snippet of what I'm working on. I won't attach any of what I have tried because, well, there are too many attempts and nothing worked $sql = ("SELECT * FROM Teamregister WHERE pkSoloregisterID=$pkSoloregisterID"); $query = mysql_query($sql); $result = mysql_query($sql); $myrow = mysql_fetch_array($result); printf ('<form action="https://www.mysite.com/cgi-bin/webscr" method="post"> <p>Your registration fee is <strong>$%s</strong>.</p>', $myrow['total_fee']); echo ("<input type='hidden' name='item_name_1' value='Registration Fee'>"); printf ('<input type="hidden" name="amount_1" value="%s">', $myrow['total_fee']); echo ("<br /><div align='center'> <input type='submit'' value='Pay Online'> </form>'); Make sense? Like I said, the values are already there but the math isn't happening. To check I tried using individual DB values instead of "total_fee" and the numbers show up. Just cannot get them to add. |