Jump to content

Displaying Date In A User Formated Style...


thara

Recommended Posts

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 by Psycho
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.