thara Posted September 26, 2012 Author Share Posted September 26, 2012 Herewith I have attached an image for your better understanding... there, you can view clearly what are the values that im getting as output.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2012 Share Posted September 26, 2012 (edited) Scratch that Edited September 26, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
thara Posted September 26, 2012 Author Share Posted September 26, 2012 I clear what you are saying.. but not idea what to do. can you tell me what I need to do to display seconds or minutes? Im sorry... Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2012 Share Posted September 26, 2012 (edited) There is a fundamental misunderstanding in this thread. Some believe that what is wanted is a concatenated output of multiple units (e.g. "1 year, 5 months, 3 days ago). However, that is not what I understand what the OP wants. I believe the OP just wants the LARGEST until measurement. So, if the time since is ~15 days then the output should be "2 weeks ago". I believe the problem the OP is having with the code is that he is testing with a FUTURE date/time which results in a negative value - which fouls up the entire logic. I have no idea what timezone the OP is in - so I can't be sure, but the test value used in his code $date = '2012-09-26 17:25:02'; has not passed yet for the US/Canada timezones I have rewritten the code in what I felt was a more logical format. function time_since ($startTimeStr, $endTimeStr=false) { //Set start and end times in seconds $endTimeInt = (!$endTimeStr) ? time() : strtotime($endTimeStr); $startTimeInt = strtotime($startTimeStr); //Calculate the difference $timeDiff = $endTimeInt - $startTimeInt; //Get the difference in seconds $tokens = array ( 'year' => 31536000, 'month' => 2592000, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1 ); foreach ($tokens as $label => $measurement) { if ($timeDiff >= $measurement) { $units = floor($timeDiff/$measurement); $plural = ($units>1) ? 's' : ''; return $units . ' ' . $label . $plural; } } return false; } $postedDateTime = '2012-09-26 12:25:02'; echo 'Mr. Tharanga added his tutor profile ' . time_since($postedDateTime) . ' ago'; Edited September 26, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
thara Posted September 26, 2012 Author Share Posted September 26, 2012 thanks Psycho.. your explanation was very straightforward for me.. Not I got the idea clearly... thanks again.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2012 Share Posted September 26, 2012 OK, I decided to extend the function a bit (I'm sure there is probably already pre-written ones to do this, but I had a few minutes to kill). The function takes three parameters. 1) $startTimeStr: The start time (as a string) for the period to be measured 2) $allUnits [Optional]: Determines if only the largest measurement is returned or the largest and all others with a non-zer0 value 3) $endTimeStr [Optional]: The end time of the period to be measured. If not passed (or false) will default to current timestamp. //Function to determine a textual representation of the time between two events function time_since ($startTimeStr, $allUnits=false, $endTimeStr=false) { //Set start and end times in seconds $endTimeInt = (!$endTimeStr) ? time() : strtotime($endTimeStr); $startTimeInt = strtotime($startTimeStr); if($endTimeInt===false || $startTimeInt===false) { return false; } //Calculate the difference $timeDiff = $endTimeInt - $startTimeInt; //Get the difference in seconds $measurements = array ( 'year' => 31536000, 'month' => 2592000, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1 ); $returnValues; foreach ($measurements as $label => $measurement) { if ($timeDiff >= $measurement) { $units = floor($timeDiff/$measurement); $plural = ($units>1) ? 's' : ''; $returnString = $units . ' ' . $label . $plural; if(!$allUnits) { return $returnString; } $timeDiff -= $units * $measurement; $returnValues[] = $returnString; } } if(!count($returnValues)) { return false; } return implode(', ', $returnValues); } ## USAGE $postedDateTime = '2012-09-24 13:14:02'; echo 'This post was updated ' . time_since($postedDateTime) . ' ago'; // This post was updated 2 days ago echo 'This post was updated ' . time_since($postedDateTime, true) . ' ago'; // This post was updated 2 days, 19 minutes, 33 seconds ago Quote Link to comment Share on other sites More sharing options...
thara Posted September 27, 2012 Author Share Posted September 27, 2012 Psycho Thank you very much for your help.. and I welcomed all other answers too and thanks for those. My problem is now solved. 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.