Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    Show relative dates

    How can I show relative date for this date format YYYY-MM-DD HH:MM:SS?

    Thanks in advance
    Last edited by levani; 05-02-2009 at 03:16 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Relative dates? of the format YYY-MM-DD HH:MM:SS?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    Sorry, YYYY of course. I corrected it

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by levani View Post
    Sorry, YYYY of course. I corrected it
    Sorry, your question is still not clear (to me only?)!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    show date in that format??

  • #6
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    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?

  • #7
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    yes...


    PHP Code:
    <?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...

  • #8
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    Thanks I'll check it out...

  • #9
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    This code doesn't display anything at all

  • #10
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    you didn't echo it

  • #11
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    What should I echo?

  • #12
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    Well I made it work. Thanks.

    How can I remove the decade from periods?

  • #13
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    How can I remove the decade from periods?
    I think it's very obvious from the code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #14
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    lol

    PHP Code:
    <?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

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •