nightkarnation Posted March 11, 2011 Share Posted March 11, 2011 Hey guys! I have the following doubt, I have a Date value and I want to add to that date 30 seconds plus. Example: $last_time = date('D M j H:i:s \G\M\TO Y'); //echo: Thu Mar 10 18:33:48 GMT-0300 2011 I need the following date: Thu Mar 10 18:33:48 GMT-0300 2011 to become: Thu Mar 10 18:34:18 GMT-0300 2011 This means that the retrieved Date now has 30 more seconds... Any ideas?? Looking forward to any help, Thanks a lot in advance, Cheers! Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted March 11, 2011 Author Share Posted March 11, 2011 Anyone? Please!! Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 by googling "php time" i found the php manual. it seems you could $addtime = time() + ( 0* 0 * 0 * 30); and call it accordingly Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted March 11, 2011 Author Share Posted March 11, 2011 Hello Mahngiel First of all, thanks a lot for your kind help. I have googled php time and php date but couldnt find what I was looking for, I am trying to implement the line of code you gave me but I cant seem to get it to work... I tried the awful code below: $addtime = time() + ( 0* 0 * 0 * 30); echo "last_time=".$last_time + $addtime; $last_time = $last_time + $addtime; Obviously is not working, any ideas and/or suggestions? Thanks a lot in advance! Quote Link to comment Share on other sites More sharing options...
jcbones Posted March 11, 2011 Share Posted March 11, 2011 Try: $last_time = date('D M j H:i:s \G\M\TO Y',strtotime('+30 seconds')); //echo: Thu Mar 10 18:33:48 GMT-0300 2011 Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted March 11, 2011 Author Share Posted March 11, 2011 Thanks a lot JcBones!! That works great!! but I have one more problem though and I really think you can fix this easily... $last_time already has the date as a value ($last_time already equals: Thu Mar 10 18:33:48 GMT-0300 2011) But now I need $last_time to +30 seconds... I tried: $last_time = date('$last_time',strtotime('+30 seconds')); But obviously is not working, any ideas and/or suggestions? Thanks a lot again! Quote Link to comment Share on other sites More sharing options...
jcbones Posted March 11, 2011 Share Posted March 11, 2011 $time = time(); $last_time = date('D M j H:i:s \G\M\TO Y',$time); //echo: Thu Mar 10 18:33:48 GMT-0300 2011 echo $last_time; $last_time_plus_30 = date('D M j H:i:s \G\M\TO Y',strtotime('+30 seconds',$time)); echo $last_time_plus_30; Will work even if there are a couple seconds execution time between the date() functions. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 11, 2011 Share Posted March 11, 2011 Take a look at the manual page for date() and strtotime() and you will see why that's not working. You have to do: <?php $last_time = 'Thu Mar 10 18:33:48 GMT-0300 2011'; $new_last_time = date('D M j H:i:s \G\M\TO Y',strtotime($l) + 30); echo $new_last_time; ?> Ken Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted March 11, 2011 Author Share Posted March 11, 2011 Guys...I am not making myself clear and I apologize for that. $last_time already equals Thu Mar 10 18:33:48 GMT-0300 2011 I am grabbing $last_time with that value already from DB... I need now to add 30 seconds to $last_time that has that date... Based on what you guys tought me, I tried this line: $last_time = date($last_time,strtotime('+30 seconds')); It is working but the date format changed a bit and I need the format to be exactly the same only with the 30 seconds added. Thu Mar 10 18:33:48 GMT-0300 2011 (Already grabbed from server) Thu Mar 10 18:34:18 GMT-0300 2011 (Is my goal with script modification) Any ideas?? Thanks a lot !! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 11, 2011 Share Posted March 11, 2011 <?php $last_time = 'Thu Mar 10 18:33:48 GMT-0300 2011'; $new_last_time = date('D M j H:i:s \G\M\TO Y',strtotime($last_time) + 30); echo $new_last_time; ?> Ken Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted March 11, 2011 Author Share Posted March 11, 2011 Ken! Thanks a lot for your help! I tried your code and I am getting the following date: Wed Dec 31 21:00:29 GMT-0300 1969 Any ideas why?? Thanks a lot! Cheers, Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 11, 2011 Share Posted March 11, 2011 I had a typo in the first code I posted. Post the code you used. Ken Quote Link to comment Share on other sites More sharing options...
nightkarnation Posted March 11, 2011 Author Share Posted March 11, 2011 This is what I tried: $result = mysql_query("SELECT Last_User_Time FROM ATS_MAIN WHERE Id = '$id'"); while($row=mysql_fetch_array($result)) { $last_time = $row['Last_User_Time']; } $new_last_time = date('D M j H:i:s \G\M\TO Y',strtotime($last_time) + 30); echo $new_last_time; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 11, 2011 Share Posted March 11, 2011 If you're only expecting one row to be returned from the query, you don't need the while loop. You should also check if you got a result from the query, if you didn't that would explain the results, since $last_time wouldn't be set. <?php $result = mysql_query("SELECT Last_User_Time FROM ATS_MAIN WHERE Id = '$id'"); if (mysql_numrows($result) > 0) { $row=mysql_fetch_assoc($result); $last_time = $row['Last_User_Time']; echo $last_time . '<br>'; // just checking $new_last_time = date('D M j H:i:s \G\M\TO Y',strtotime($last_time) + 30); echo $new_last_time; } else { echo "No data returned from the query"; } ?> Ken Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 If you just need to add 30 seconds to the time stored in the database you may find it easier and more efficient to do it directly in the query. ]SELECT DATE_ADD(Last_User_Time, INTERVAL 30 SECOND) AS Last_User_Time FROM ATS_MA . . . Quote Link to comment 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.