Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date Format - "X days hours minutes ago"

Posted on 2011-09-03
4
Medium Priority
?
478 Views
Last Modified: 2012-06-22
something that i saw on some of the news site/ music sites
date format listed as descending:

for instance instead of

"Published on 30 August 2011"

"Published 5 days  17 hours 5 minutes ago"


or like "25 minutes old"


is this possible for joomla articles?

joomla 1.7.0


i have checked http://us.php.net/strftime first
0
Comment
Question by:Anti-Mhz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 36479758
There are many solutions to this problem on the php time() page. see http://www.php.net/manual/en/function.time.php

Here is one:
This is another function to calculate the difference between two times and output it in a relative format. This one, however, combines various units. In other words, it will return "3 days, 2 hours, 6 minutes, and 2 seconds ago" instead of "3 days ago."

Please bear in mind that it is intended for times that are very close together. It will not be accurate for calculating dates more than a month apart. This is because one of the units it relies on is "weeks," which do not correspond directly with months. What I'm trying to get across is, the program assumes that a month is precisely 4 weeks long, i.e. 28 days. This also means that a "year" according to this function is 336 days.

<?php
 function rel_time($from, $to = null)
 {
  $to = (($to === null) ? (time()) : ($to));
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
  $from = ((is_int($from)) ? ($from) : (strtotime($from)));

  $units = array
  (
   "year"   => 29030400, // seconds in a year   (12 months)
   "month"  => 2419200,  // seconds in a month  (4 weeks)
   "week"   => 604800,   // seconds in a week   (7 days)
   "day"    => 86400,    // seconds in a day    (24 hours)
   "hour"   => 3600,     // seconds in an hour  (60 minutes)
   "minute" => 60,       // seconds in a minute (60 seconds)
   "second" => 1         // 1 second
  );

  $diff = abs($from - $to);
  $suffix = (($from > $to) ? ("from now") : ("ago"));

  foreach($units as $unit => $mult)
   if($diff >= $mult)
   {
    $and = (($mult != 1) ? ("") : ("and "));
    $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s"));
    $diff -= intval($diff / $mult) * $mult;
   }
  $output .= " ".$suffix;
  $output = substr($output, strlen(", "));

  return $output;
 }
?>

Open in new window


The function rel_time() accepts two parameters: $from and $to. For best results, provide them as UNIX timestamps (derived from PHP's time() function). They also accept formats supported by strtotime(). $to is an optional argument and defaults to the current time.

The function will calculate the difference between $from and $to. If $from occurs after $to, the function will substitue "ago" with "from now."

Example usage:
<?php
 echo rel_time("September 2, 2010 4:20 PM");
 // returns "2 weeks, 4 days, 23 hours, 25 minutes, and 3 seconds from now"
?> 

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36481334
For some examples of how to do this and several other interesting things with DATETIME values, please see the peer-reviewed article here.  In particular you might be interested in "Practical Application #3, and the discussion contained in the paragraph entitled "Ambiguity of the term 'Month'."
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best
0
 

Expert Comment

by:rickyponting
ID: 36541673
call function like : dateTimeDiff('2010-09-02 00:00:00');

give answer like x year x month x days x hours ago

 
function dateTimeDiff($data_ref){

// Get the current date
$current_date = date('Y-m-d H:i:s');

// Extract from $current_date
$current_year = substr($current_date,0,4);
$current_month = substr($current_date,5,2);
$current_day = substr($current_date,8,2);

// Extract from $data_ref
$ref_year = substr($data_ref,0,4);
$ref_month = substr($data_ref,5,2);
$ref_day = substr($data_ref,8,2);

// create a string yyyymmdd 20071021
$tempMaxDate = $current_year . $current_month . $current_day;
$tempDataRef = $ref_year . $ref_month . $ref_day;

$tempDifference = $tempMaxDate-$tempDataRef;

// If the difference is GT 10 days show the date
if($tempDifference >= 10){
echo $data_ref;
} else {

// Extract $current_date H:m:ss
$current_hour = substr($current_date,11,2);
$current_min = substr($current_date,14,2);
$current_seconds = substr($current_date,17,2);

// Extract $data_ref Date H:m:ss
$ref_hour = substr($data_ref,11,2);
$ref_min = substr($data_ref,14,2);
$ref_seconds = substr($data_ref,17,2);

$hDf = $current_hour-$ref_hour;
$mDf = $current_min-$ref_min;
$sDf = $current_seconds-$ref_seconds;

// Show time difference ex: 2 min 54 sec ago.
//if($dDf<1){
if($hDf>0){
if($mDf<0){
$mDf = 60 + $mDf;
$hDf = $hDf - 1;
echo $mDf . ' min ago';
} else {
echo $hDf. ' hr ' . $mDf . ' min ago';
}
} else {
if($mDf>0){
echo $mDf . ' min ' . $sDf . ' sec ago';
} else {
echo $sDf . ' sec ago';
}
}
//} //else {
//echo $dDf . ' days ago';
//}
}
}

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36542289
Please see http://www.laprbass.com/RAY_temp_rickyponting.php
I copied and installed the function posted at ID:36541673 and used the test case written in that comment.  You can see the output for yourself.  This is an object lesson in why it is a good idea to test the proposed solutions that are posted at EE.  Not all of them have been tested by the authors, and many of them have not been peer-reviewed.  Caveat emptor.

As daVinci wrote, "Simplicity is the ultimate sophistication."  The number of seconds in a minute, hour, day, or week is a constant.  The number of seconds in a month or year is not a constant (February and Leap year).  So DATETIME mathematics needs to deal with that issue.  Fortunately PHP has built-in functions that handle most of it.  Since the language is open-source, the DATETIME functions have been scrutinized by many of the top computer scientists at the finest universities around the world.  I tend to trust their knowledge, skills and abilities.  So I prefer to use the built-in functions, as shown in the article linked above.
<?php // RAY_temp_rickyponting.php
error_reporting(E_ALL);

// COPIED FROM EE
function dateTimeDiff($data_ref){

// Get the current date
$current_date = date('Y-m-d H:i:s');

// Extract from $current_date
$current_year = substr($current_date,0,4);
$current_month = substr($current_date,5,2);
$current_day = substr($current_date,8,2);

// Extract from $data_ref
$ref_year = substr($data_ref,0,4);
$ref_month = substr($data_ref,5,2);
$ref_day = substr($data_ref,8,2);

// create a string yyyymmdd 20071021
$tempMaxDate = $current_year . $current_month . $current_day;
$tempDataRef = $ref_year . $ref_month . $ref_day;

$tempDifference = $tempMaxDate-$tempDataRef;

// If the difference is GT 10 days show the date
if($tempDifference >= 10){
echo $data_ref;
} else {

// Extract $current_date H:m:ss
$current_hour = substr($current_date,11,2);
$current_min = substr($current_date,14,2);
$current_seconds = substr($current_date,17,2);

// Extract $data_ref Date H:m:ss
$ref_hour = substr($data_ref,11,2);
$ref_min = substr($data_ref,14,2);
$ref_seconds = substr($data_ref,17,2);

$hDf = $current_hour-$ref_hour;
$mDf = $current_min-$ref_min;
$sDf = $current_seconds-$ref_seconds;

// Show time difference ex: 2 min 54 sec ago.
//if($dDf<1){
if($hDf>0){
if($mDf<0){
$mDf = 60 + $mDf;
$hDf = $hDf - 1;
echo $mDf . ' min ago';
} else {
echo $hDf. ' hr ' . $mDf . ' min ago';
}
} else {
if($mDf>0){
echo $mDf . ' min ' . $sDf . ' sec ago';
} else {
echo $sDf . ' sec ago';
}
}
//} //else {
//echo $dDf . ' days ago';
//}
}
}

// ADDED TO TEST THE FUNCTION
$x = dateTimeDiff('2010-09-02 00:00:00');
var_dump($x);

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question