SkyRanger Posted April 19, 2007 Share Posted April 19, 2007 I am currently using datetime in mysql was wondering if somebody could point me to a good tutorial so I can figure out how to get: $timeago = $datetime - now() Where $timeago will echo something like Posted: 16 minutes ago or 1 day 4 hrs ago etc. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/ Share on other sites More sharing options...
JSHINER Posted April 19, 2007 Share Posted April 19, 2007 Try this out: $now = date("Y-m-d H:i:s"); $timeago = ($now - $datetime); $result =date("H:i:s",strtotime($timeago)); Which should display how many hours, min, sec ago... example: 12:15:12 (is 12 hours, 15 min, 12 seconds ago)... so there is more to do, however I hope thats a start. You can contact Kevin Rose, Digg seems to have a good working model of this NOTE: I did not test this, and I'm quite tired - but in my head it works - let me know. Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-232881 Share on other sites More sharing options...
SkyRanger Posted April 19, 2007 Author Share Posted April 19, 2007 Yeah it is a start, thanks Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233103 Share on other sites More sharing options...
SkyRanger Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, not sure if I am doing this right, actually I don't think I am doing this right because I am not getting any errors or even getting anything to display: $timestamp = $rowb2["bdate"]; $difference = time() - $timestamp; $btime = ""; if($difference < 60) return $difference." seconds ago"; else{ $difference = round($difference / 60); if($difference < 60) return $difference." minutes ago"; else{ $difference = round($difference / 60); if($difference < 24) return $difference." hours ago"; else{ $difference = round($difference / 24); if($difference < 7) return $difference." days ago"; else{ $difference = round($difference / 7); return $difference." weeks ago"; } } } } echo $btime; The entry in bdate is : 2007-04-18 22:46:13 and another is: 2007-04-17 09:06:24 I have myself so confused now...lol Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233147 Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 You're using the "return" statement, so I'm assuming this code is in a function. How are you using the function? Ken Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233149 Share on other sites More sharing options...
SkyRanger Posted April 19, 2007 Author Share Posted April 19, 2007 That could be where the problem is...lol page would be as: function timewhen() { ..$btime = ""; code here } function main () { include timewhen(); echo "Last Post was:"; echo $btime; which I know is wrong or I wouldn't be here...lol This is the first time I even attempted something like this. Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233155 Share on other sites More sharing options...
obsidian Posted April 19, 2007 Share Posted April 19, 2007 Try something like this: <?php function timeago($time) { $min = 60; $hr = 60 * $min; $day = 24 * $hr; $now = time(); $diff = array(); $timediff = abs($time - $now); $diff['days'] = floor($timediff / $day); $diff['hours'] = floor(($timediff % $day) / $hr); $diff['minutes'] = floor((($timediff % $day) % $hr) / $min); $diff['seconds'] = (($timediff % $day) % $hr) % $min; $out = ''; foreach ($diff as $k => $v) { if ($v > 0) $out .= "$v $k, "; } $out = substr($out, 0, strlen($out) - 2); return $out; } $date = "2007-02-14 8:15:02"; $time = strtotime($date); echo timeago($time); ?> Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233169 Share on other sites More sharing options...
SkyRanger Posted April 19, 2007 Author Share Posted April 19, 2007 Awsome, thanks Obsidian, that code worked exactly the way I needed it to. Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233175 Share on other sites More sharing options...
SkyRanger Posted April 19, 2007 Author Share Posted April 19, 2007 Ok that works awesome Would I do the same if I wanted it to show for example $date > "2007-04-12 8:15:02"; - echo 1 week ago or $date > "2007-04-18 8:15:02"; - echo 1 Day Ago or $date > "2007-04-19 8:15:02"; - echo 3 hours 4 minutes ago Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233226 Share on other sites More sharing options...
obsidian Posted April 19, 2007 Share Posted April 19, 2007 Ok that works awesome Would I do the same if I wanted it to show for example $date > "2007-04-12 8:15:02"; - echo 1 week ago or $date > "2007-04-18 8:15:02"; - echo 1 Day Ago or $date > "2007-04-19 8:15:02"; - echo 3 hours 4 minutes ago My function will handle any of the above scenarios, but it will count it down to the second. If it hasn't been a full day yet, it starts the count with hours. Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233272 Share on other sites More sharing options...
SkyRanger Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, thanks, time to start tearing hair out. Do not be surprised to see me back here...lol. But your code looks really simple to work with. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233279 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.