lynxus Posted June 1, 2009 Share Posted June 1, 2009 Hi guys, how would i do the below? $time = "2009-06-01 19:22:30"; if ($time OLDERthan1Minute ) { echo "Im old"; }else{ echo "Im younger than 1 minute"; } Thanks G Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/ Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 if (strtotime($time)-60 > time()) { echo 'within the last minute'; } Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847194 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 OK that works ( ithink. ) This is what im attempting, but doesnt seem to work. im doing a select blaa,blaaa,CURRENT_DATE from blaaa; $time = $row['lastseen']; $cdate = $row['current_date']; echo $cdate; if (strtotime($time)-30 > $cdate) { echo '<td><img src="Images/useronline.png" border="0" /></td>'; }else{ echo '<td><img src="Images/useroffline.png" border="0" /></td>'; } What am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847204 Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 Why not just use time instead of $_row['current_date']? Have you checked that it contains a UNIX timestamp? By the way, 30 seconds doesn't seem like much for an online/offline indicator. Sure you don't mean 30*60? Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847205 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 The problem is the date on the mysql db will be out compared to the users current time? or am i over thinking this? Whats the best way to achieve what im doing? Thanks so much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847207 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 using the time() doesnt seem to work. Heres the format on the mysql db.. lastseen - 2009-06-01 20:53:07 Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847209 Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 time() contains the current time (obviously). Just compare the lastseen with the current time. The lastseen has to be converted to a UNIX timestamp which is what strtotime does. if (strtotime($time)-30*60 > time()) { echo '<td><img src="Images/useronline.png" border="0" /></td>'; }else{ echo '<td><img src="Images/useroffline.png" border="0" /></td>'; } should do it (assuming you mean 30 minutes and not 30 seconds). Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847211 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 I mean 30 seconds. The user updates the mysql timestamp every 10 seconds currently. Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847212 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 Humm using. if (strtotime($time)-30 > time()) { echo '<td><img src="Images/useronline.png" border="0" /></td>'; }else{ echo '<td><img src="Images/useroffline.png" border="0" /></td>'; } doesnt work. it still shows the offline image. The timestamp is being updated in the db, and it is in the var as i can echo it. Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847215 Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 Sorry, try with + instead of -. My bad. Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847219 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 Fyi, this is what i see if i echo this: echo $time; echo "<br>"; echo time(); echo "<br>"; echo strtotime($time); echo "<br>"; 2009-06-01 20:54:39 1243886885 1243886079 Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847220 Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 Right, well, your sample data isn't very good either. There is a 806 second = 13 mins 26 secs difference between the timestamp and when you requested the page. Either way, doing addition instead of subtraction as explained above should fix it. Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847224 Share on other sites More sharing options...
lynxus Posted June 1, 2009 Author Share Posted June 1, 2009 It has fixed it!! AWESOME!!!!!! Thanks so much. +50000 internets to you sir. Thanks again. -G Quote Link to comment https://forums.phpfreaks.com/topic/160527-solved-if-time-is-older-than-1-minute/#findComment-847227 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.