levani
05-02-2009, 01:19 PM
How can I show relative date for this date format YYYY-MM-DD HH:MM:SS?
Thanks in advance
Thanks in advance
|
||||
Show relative dateslevani 05-02-2009, 01: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, 02:02 PM Relative dates? of the format YYY-MM-DD HH:MM:SS? levani 05-02-2009, 03:16 PM Sorry, YYYY of course. I corrected it :) abduraooft 05-02-2009, 03:19 PM Sorry, YYYY of course. I corrected it :) Sorry, your question is still not clear (to me only?)! sea4me 05-02-2009, 03:24 PM show date in that format?? levani 05-02-2009, 03: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, 03: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, 03:31 PM Thanks I'll check it out... levani 05-02-2009, 03:44 PM This code doesn't display anything at all :( sea4me 05-02-2009, 03:45 PM you didn't echo it :p levani 05-02-2009, 03:47 PM What should I echo? levani 05-02-2009, 04:19 PM Well I made it work. Thanks. How can I remove the decade from periods? abduraooft 05-02-2009, 04:23 PM How can I remove the decade from periods? I think it's very obvious from the code. sea4me 05-03-2009, 05: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