Jamz Posted March 11, 2011 Share Posted March 11, 2011 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 Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/ Share on other sites More sharing options...
RussellReal Posted March 11, 2011 Share Posted March 11, 2011 uhm, date_add would probably work for you Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186432 Share on other sites More sharing options...
Jamz Posted March 11, 2011 Author Share Posted March 11, 2011 Hmm think thats another SQL function that I dont really need... This is the code I have so far, and I would like the added time to be output into $till_time. You can see in the code I already get an output, but I would like the full output and not just the hour <?php dbconnect(); // Include function for database connection $result = mysql_query("SELECT * FROM booking WHERE date='$date' and store='$store' ORDER BY time") or die ('Could not select ' . mysql_error()); echo '<table class="cal" width="500px"><tr><th>Bookings</th></tr><tr><td>'; if(mysql_num_rows($result)==0){ echo "Nothing Booked In Today"; }else{ echo '<table width="660px" border="1"><tr> <th>Time</th><th>Name</th><th>Car</th> <th>Description</th><th width="100%">Comments</th><th>Paid?</th> <th>Contact</th><th>Misc</th></tr>'; while($row = mysql_fetch_array($result)){ $till_time = $row['time'] + $row['duration']; echo' <tr> <td>'.$row['time'].'<br>till<br>'.$till_time.'</td> <td>'.$row['customer_name'].'</td> <td>'.$row['car_make'].' '.$row['car_model'].'<br>'.$row['car_year'].'</td> <td>'.$row['work_desc'].'</td> <td>'.$row['comments'].'</td> <td>'.$row['paid'].'</td> <td>'.$row['contact'].'</td> <td>Edit<br>Cancel</td> </tr>'; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186433 Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 SELECT DATE_ADD(start, INTERVAL TIME_TO_SEC(duration) SECOND) AS end_time FROM table Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186440 Share on other sites More sharing options...
Jamz Posted March 11, 2011 Author Share Posted March 11, 2011 So how would I implement that in my code above? Just add another SQL query? Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186442 Share on other sites More sharing options...
silkfire Posted March 11, 2011 Share Posted March 11, 2011 What do you mean by "full output"? Doesn't your code already add the times up? Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186449 Share on other sites More sharing options...
RussellReal Posted March 12, 2011 Share Posted March 12, 2011 select what pikachu's query is selecting Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186455 Share on other sites More sharing options...
Psycho Posted March 12, 2011 Share Posted March 12, 2011 Change your query as follows and this should get you an additional field returned in your db results called 'till_time' $query = "SELECT *, DATE_ADD(start, INTERVAL TIME_TO_SEC(duration) SECOND) AS till_time FROM booking WHERE date='$date' AND store='$store' ORDER BY time"; $result = mysql_query($query) or die ('Could not select ' . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186481 Share on other sites More sharing options...
Jamz Posted March 12, 2011 Author Share Posted March 12, 2011 Is there anyway to get it to output in an array: ie: <?php dbconnect(); // Include function for database connection $result = mysql_query("SELECT *, DATE_ADD(time, INTERVAL TIME_TO_SEC(duration) SECOND) AS till_time FROM booking WHERE date='$date' and store='$store' ORDER BY time") or die ('Could not select ' . mysql_error()); while($row = mysql_fetch_array($result)){ echo' <tr> <td>'.$row['time'].'<br>till<br>'.$row['till_time'].'</td> <td>'.$row['customer_name'].'</td> <td>'.$row['car_make'].' '.$row['car_model'].'<br>'.$row['car_year'].'</td> </tr>'; } As what ive just added <?php $row['till_time']; ?> does not output anything Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186579 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2011 Share Posted March 12, 2011 DATE_ADD() expects a DATE or DATETIME for the first parameter and produced an error in my query browser - Incorrect datetime value: '16:30:00'. Use this for that part of the query - SEC_TO_TIME(TIME_TO_SEC(time) + TIME_TO_SEC(duration)) AS till_time Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186583 Share on other sites More sharing options...
Jamz Posted March 12, 2011 Author Share Posted March 12, 2011 DATE_ADD() expects a DATE or DATETIME for the first parameter and produced an error in my query browser - Incorrect datetime value: '16:30:00'. Use this for that part of the query - SEC_TO_TIME(TIME_TO_SEC(time) + TIME_TO_SEC(duration)) AS till_time Perfect, works as it should Cheers for that Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186589 Share on other sites More sharing options...
Pikachu2000 Posted March 12, 2011 Share Posted March 12, 2011 Ah, see I was thinking the start field would be a DATETIME field so you'd get the correct value if the event ran past midnight. Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186608 Share on other sites More sharing options...
RussellReal Posted March 12, 2011 Share Posted March 12, 2011 ^^ thought so too Link to comment https://forums.phpfreaks.com/topic/230379-adding-time/#findComment-1186702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.