...

View Full Version : Show relative dates



levani
05-02-2009, 02:19 PM
How can I show relative date for this date format YYYY-MM-DD HH:MM:SS?

Thanks in advance

abduraooft
05-02-2009, 03:02 PM
Relative dates? of the format YYY-MM-DD HH:MM:SS?

levani
05-02-2009, 04:16 PM
Sorry, YYYY of course. I corrected it :)

abduraooft
05-02-2009, 04:19 PM
Sorry, YYYY of course. I corrected it :)
Sorry, your question is still not clear (to me only?)!

sea4me
05-02-2009, 04:24 PM
show date in that format??

levani
05-02-2009, 04:25 PM
After I get the date from database in the format above, I want to display how much time has passed after this date. For example: "1 minute ago", "3 hours ago", "2 months ago" "Yesterday" and so on.

Am I clear now? :)

sea4me
05-02-2009, 04:28 PM
yes...



<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}

$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");

$now = time();
$unix_date = strtotime($date);

// check validity of date
if(empty($unix_date)) {
return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";

} else {
$difference = $unix_date - $now;
$tense = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
$periods[$j].= "s";
}

return "$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45:30";
$result = nicetime($date); // 2 days ago

?>

untested...

levani
05-02-2009, 04:31 PM
Thanks I'll check it out...

levani
05-02-2009, 04:44 PM
This code doesn't display anything at all :(

sea4me
05-02-2009, 04:45 PM
you didn't echo it :p

levani
05-02-2009, 04:47 PM
What should I echo?

levani
05-02-2009, 05:19 PM
Well I made it work. Thanks.

How can I remove the decade from periods?

abduraooft
05-02-2009, 05:23 PM
How can I remove the decade from periods? I think it's very obvious from the code.

sea4me
05-03-2009, 06:24 AM
lol


<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}

$periods = array("second", "minute", "hour", "day", "week", "month", "year", "");
$lengths = array("60","60","24","7","4.35","12","10");

$now = time();
$unix_date = strtotime($date);

// check validity of date
if(empty($unix_date)) {
return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";

} else {
$difference = $unix_date - $now;
$tense = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
$periods[$j].= "s";
}

return "$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45:30";
$result = nicetime($date); // 2 days ago

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum