Jump to content

[SOLVED] Convert datetime into x day y hr z min ago


limitphp

Recommended Posts

I've noticed alot of sites convert certain dates, especially for comments into a format like "x day y hr z min ago".

It seems pretty neat.

I have a date that I store in mysql, type is datetime, so the format is:

2008-11-12 12:30:02

Is there like a standard way to convert this?

 

Thanks

 

btw I think someone in the user comments of date submitted a function that pretty much does what you're wanting.  Might want to read through it, save you the trouble.

 

Great, I will definitely read through that.  Thanks for the info.

Here's the code....

function ezDate($d) {
$ts = time() - strtotime(str_replace("-","/",$d));
    if($ts>31536000) $val = round($ts/31536000,0).' year';
    else if($ts>2419200) $val = round($ts/2419200,0).' month';
    else if($ts>604800) $val = round($ts/604800,0).' week';
    else if($ts>86400) $val = round($ts/86400,0).' day';
    else if($ts>3600) $val = round($ts/3600,0).' hour';
    else if($ts>60) $val = round($ts/60,0).' minute';
    else $val = $ts.' second';
    if($val>1) $val .= 's';
    return $val;
    }

    $newCommentDate = ucwords(ezDate($commentDate)).' Ago';

 

Worked like a charm!.....actually, it was less code than I thought it might take....

Awesome!....

Sometimes, I feel ike a jackace copying digg on alot of their things, but, if your going to get influence or copy, might as well copy someone who's really good.

 

Thanks again...

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.