limitphp Posted November 13, 2008 Share Posted November 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/ Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 You can use strtotime to convert your date/time string to a unix timestamp, subtract it from the current timestamp returned from using time and use date to format the timestamp to say what you want it to say. Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689354 Share on other sites More sharing options...
limitphp Posted November 13, 2008 Author Share Posted November 13, 2008 When I'm comparing the two values, is the difference in seconds? Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689379 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 yes. Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689390 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689396 Share on other sites More sharing options...
limitphp Posted November 13, 2008 Author Share Posted November 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689397 Share on other sites More sharing options...
limitphp Posted November 13, 2008 Author Share Posted November 13, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689405 Share on other sites More sharing options...
limitphp Posted November 13, 2008 Author Share Posted November 13, 2008 nevermind, I forgot to put in a date!.... sorry..... Quote Link to comment https://forums.phpfreaks.com/topic/132571-solved-convert-datetime-into-x-day-y-hr-z-min-ago/#findComment-689434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.