Tenaciousmug Posted May 11, 2011 Share Posted May 11, 2011 Ok, I have a variable ($incubation) set as 04:00:00. Then I have another variable ($starttime) set to the current time. Both are printing out fine. But I'm trying to get an $endtime from adding the incubation time to the start time. $incubation = $row['incubation']; //IM GRABBING THIS TIME FROM THE DATABASE. It prints 04:00:00 $starttime = date("H:i:s"); //prints 16:23:39 $endtime = date("H:i:s", $starttime+$incubation); //prints 20:00:00 when it's suppose to print 20:23:39 Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/ Share on other sites More sharing options...
requinix Posted May 11, 2011 Share Posted May 11, 2011 $incubation = "04:00:00"; list($h, $m, $s) = explode(":", $incubation); $starttime = time(); $endtime = $starttime + (3600*$h + 60*$m + $s); echo date("H:i:s", $endtime); Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214061 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 If you want to do this in PHP, you want to convert the strings to unix timestamps, using strtotime() If you want to do this in SQL, you probably want to use SELECT DATE_FORMAT( ADDTIME(`start`,`incubation`), '%W %M %Y %H:%i:%s') as `finish` FROM `table` Alternately, You can use UNIX_TIMESTAMP(`start`) and TIME_TO_SEC(`time`) as a better way to solve this. Oops. I see you don't store your start time. Well, replace instances of `start` with NOW() in SQL Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214064 Share on other sites More sharing options...
Tenaciousmug Posted May 11, 2011 Author Share Posted May 11, 2011 Requinix, your result gives me : 20:57:26. It's suppose to be 8:57:26. Xyph, your result gives me : 04:00:00 EDIT I even tried it this way: $incubation = $row['incubation']; $incubation = strtotime($incubation); $starttime = date("H:i:s"); $starttime = strtotime($starttime); $endtime = $starttime+$incubation; $endtime = date("H:i:s", $endtime); echo $endtime; Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214080 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 My result won't be something you can just copy and paste into your code. I'm trying to help you find the best solution, not code for you. If you change your query to return `incubation` in seconds (using TIME_TO_SEC()) then you can simply use echo date('H:i:s', time() + $row['incubation_seconds']) SELECT TIME_TO_SEC(`incubation`) as `incubation_seconds` FROM `table` Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214085 Share on other sites More sharing options...
Tenaciousmug Posted May 11, 2011 Author Share Posted May 11, 2011 $sql = "SELECT TIME_TO_SEC('incubation') as 'incubation_seconds' FROM eggs WHERE eggid='".$_POST['eggid']."'"; $result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn)); $row = mysqli_fetch_assoc($result); $endtime = date("H:i:s", time()+$row['incubation_seconds']); echo $endtime; It's displaying today's current time. And I'm not going to learn a code that doesn't work. I learn from codes that work because I read over them and then I finally understand what it's doing.. EDIT $sql = "SELECT incubation FROM eggs WHERE eggid='".$_POST['eggid']."'"; $result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn)); $row = mysqli_fetch_assoc($result); $incubation = $row['incubation']; $incubation = strtotime($incubation); $starttime = date("H:i:s"); $endtime = date("H:i:s", time()+$incubation); echo $endtime; And this one works on the minutes and seconds, but it's setting the hours to 0 EDIT WAIT, I take that back. It's jumping 3 more hours onto each incubation time. Why is it doing that? Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214092 Share on other sites More sharing options...
requinix Posted May 11, 2011 Share Posted May 11, 2011 Requinix, your result gives me : 20:57:26. It's suppose to be 8:57:26. :| Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214103 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 Ahem. 'incubation' != `incubation` Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214113 Share on other sites More sharing options...
Tenaciousmug Posted May 11, 2011 Author Share Posted May 11, 2011 Ohhh. Sorry. Thanks! It's working now! (: But, is there anyway I can convert the incubation time to say 4 hours or would I have to do another sql statement since that one converted it into seconds already? Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214126 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 SELECT TIME_TO_SEC('incubation') as `incubation_seconds` HOUR(`incubation`) as `hours` MINUTE(`incubation`) as `minutes` // ETC FROM eggs WHERE eggid='".mysqli_escape_string($cxn,$_POST['eggid'])."'" Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214134 Share on other sites More sharing options...
Tenaciousmug Posted May 11, 2011 Author Share Posted May 11, 2011 Nevermind, I got it. (: I just typed this statement: $hours = intval(intval($row['incubation_seconds']) / 3600); Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214136 Share on other sites More sharing options...
Tenaciousmug Posted May 12, 2011 Author Share Posted May 12, 2011 Ok I'm sorry to bring this back up. But I just realized I needed a DATETIME instead of TIME. I've been trying for the last hour to get it to work.. I just can't. I thought it would be easy since I understood the time, but I just can't do it.. { $sql = "SELECT incubation FROM eggs WHERE eggid='".$_POST['eggid']."'"; $result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn)); $row = mysqli_fetch_assoc($result); $incubation = $row['incubation']; echo $incubation."<br>"; $starttime = date("Y-m-d H:i:s")."<br>"; print_r($starttime); $endtime = date("Y-m-d H:i:s", $starttime+$incubation); print_r($endtime); $sql2 = "UPDATE incubator SET status='incubating', starttime='".$starttime."', endtime='".$endtime."' WHERE userid='".$_SESSION['userid']."' AND eggid='".$_POST['eggid'].""; mysqli_query($cxn, $sql2); $hours = intval(intval($row['incubation_seconds']) / 3600); $error = "<p align=\"center\">You have started the incubation. You're egg will be fully incubated in ".$hours." hours.</p>"; } The endtime keeps coming out : 1969-12-31 19:33:31 ... It's definitely not 1969.. o.o Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214251 Share on other sites More sharing options...
MadTechie Posted May 12, 2011 Share Posted May 12, 2011 that because you have the $starttime+$incubation, remember $starttime is now a string, so you need to convert to a time or as its the currect time just use time() ie $starttime = date("Y-m-d H:i:s")."<br>"; print_r($starttime); $endtime = date("Y-m-d H:i:s", time()+$incubation); print_r($endtime); EDIT also i am not sure if this topic is solved or not ! if its not then click "Topic Not solved" at the bottom.. or people will skip the thread Quote Link to comment https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/#findComment-1214448 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.