
Anti-Mhz
asked on
Date Format - "X days hours minutes ago"
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
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

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'."
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Best
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Best

call function like : dateTimeDiff('2010-09-02 00:00:00');
give answer like x year x month x days x hours ago
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';
//}
}
}
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Here is one:
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:
Open in new window