supanoob Posted July 27, 2008 Share Posted July 27, 2008 I am trying to use time() to see when so many seconds have past since a last action. However when i use time() i get the following 1217191386 . If there a way to turn that into the correct time? Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/ Share on other sites More sharing options...
thebadbad Posted July 27, 2008 Share Posted July 27, 2008 Just save the seconds returned by time() when the action happens, and then do a subtraction to see how long ago it happened: <?php $saved_time = 1217191386; //e.g. $diff = time() - $saved_time; echo "Action happened $diff seconds ago."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601069 Share on other sites More sharing options...
supanoob Posted July 27, 2008 Author Share Posted July 27, 2008 yeah i have but when i save them they go into the database in a correct format eg: HH:MM:SS Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601076 Share on other sites More sharing options...
Nhoj Posted July 27, 2008 Share Posted July 27, 2008 <?php $time = time(); $hms = date('H:i:s', $time); echo $hms; ?> http://us3.php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601083 Share on other sites More sharing options...
supanoob Posted July 27, 2008 Author Share Posted July 27, 2008 Thanks, still doesnt allow me to see if the new actions was at least 30 seconds after the first one, but never mind, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601097 Share on other sites More sharing options...
GingerRobot Posted July 27, 2008 Share Posted July 27, 2008 Well, you'll need to do a comparison then won't you? You'll need both times in the form of integers. So you'll have to use strtotime()/mktime() to convert your dates to unix timestamps. If you're trying to do it in a mysql query, use the mysql UNIX_TIMESTAMP() function. Perhaps if you explain your situation more fully, we'll be able to give you more help. Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601105 Share on other sites More sharing options...
thebadbad Posted July 27, 2008 Share Posted July 27, 2008 So an action never happened the previous day? If no, you can convert the HH:MM:SS format to a timestamp with strtotime(). The function assumes the provided time is today, so we won't need to supply todays date: <?php $saved_time = '12:00:00'; $time = strtotime($saved_time); $diff = time() - $time; echo "Action happened $diff seconds ago."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601114 Share on other sites More sharing options...
supanoob Posted July 27, 2008 Author Share Posted July 27, 2008 Well basically i have a forum, i want to make it so each person can post once every 30 seconds. I have the persons last post time saved into the db, i then want to compare both the current time and last post time to see if 30 seconds has passed yet. if not i want to say so and stop the post being made. Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601115 Share on other sites More sharing options...
supanoob Posted July 27, 2008 Author Share Posted July 27, 2008 So an action never happened the previous day? If no, you can convert the HH:MM:SS format to a timestamp with strtotime(). The function assumes the provided time is today, so we won't need to supply todays date: <?php $saved_time = '12:00:00'; $time = strtotime($saved_time); $diff = time() - $time; echo "Action happened $diff seconds ago."; ?> That has sorted it, thanks alot. Topic solved Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601120 Share on other sites More sharing options...
thebadbad Posted July 27, 2008 Share Posted July 27, 2008 But if a user's latest post was made some day in the past (will happen all the time, obviously), you'll need to save the date also. Or simply the timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601127 Share on other sites More sharing options...
supanoob Posted July 27, 2008 Author Share Posted July 27, 2008 But if a user's latest post was made some day in the past (will happen all the time, obviously), you'll need to save the date also. Or simply the timestamp. yeah i do, when a post is made the time is saved. I think i see what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/116888-solved-time/#findComment-601144 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.