Page 1 of 3 123 LastLast
Results 1 to 10 of 30
  1. #1
    Join Date
    Jul 2005
    Location
    Leeds
    Posts
    947
    Tokens
    0
    Habbo
    Flumples

    Default PHP to display number of days since given date?

    I have very little PHP knowledge, usually I can find anything out with a google search but can't find this anywhere.

    I need to display the number of days since a given date, basically I need a counter that increases by 1 each day.

    Any ideas?

  2. #2
    Join Date
    Sep 2009
    Location
    Hull
    Posts
    827
    Tokens
    0

    Latest Awards:

    Default

    I made this, it basically works abit like facebooks timer (under status' & posts etc..)

    Edit it to how you need it

    PHP Code:
    function howLong($timestamp) {
        
    $diff time()-$timestamp;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        return 
    "on " date("jS F, Y"$timestamp);

    Enjoy, Lew.
    Im not here to be loved, I love to be hated :-}


  3. #3
    Join Date
    Jul 2005
    Location
    Leeds
    Posts
    947
    Tokens
    0
    Habbo
    Flumples

    Default

    Thanks! You're gonna have to help me to get it to display on a page though, I really am SUCH a php noob!! I've tried wrapping <?php and ?> tags round it but didn't work, probably doing it all wrong

  4. #4
    Join Date
    Jul 2004
    Location
    California
    Posts
    8,725
    Tokens
    3,789
    Habbo
    HotelUser

    Latest Awards:

    Default

    Quote Originally Posted by Flumples View Post
    Thanks! You're gonna have to help me to get it to display on a page though, I really am SUCH a php noob!! I've tried wrapping <?php and ?> tags round it but didn't work, probably doing it all wrong

    try this:

    PHP Code:
    <?php
    function howLong($timestamp) {
        
    $diff time()-$timestamp;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        return 
    "on " date("jS F, Y"$timestamp);
    }  

    howLong(time());
    ?>
    I'm not crazy, ask my toaster.

  5. #5
    Join Date
    Jun 2008
    Location
    United Kingdom
    Posts
    2,015
    Tokens
    568

    Latest Awards:

    Default

    Quote Originally Posted by LMS16 View Post
    I made this, it basically works abit like facebooks timer (under status' & posts etc..)

    Edit it to how you need it

    PHP Code:
    function howLong($timestamp) {
        
    $diff time()-$timestamp;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        return 
    "on " date("jS F, Y"$timestamp);

    Enjoy, Lew.
    That's not what he asked for though, he wants to display the number of days, not seconds/minutes/hours.

    Quote Originally Posted by HotelUser View Post
    try this:

    PHP Code:
    <?php
    function howLong($timestamp) {
        
    $diff time()-$timestamp;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        return 
    "on " date("jS F, Y"$timestamp);
    }  

    howLong(time());
    ?>
    That wouldn't work, it says time() - time().

    I'd do this:
    PHP Code:
    function plural($num) {
        if (
    $num != 1)
            return 
    "s";
    }
     
    function 
    getRelativeTime($date) {
        
    $diff time() - $date;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        if (
    $diff<7)
            return 
    $diff " day" plural($diff) . " ago";
        
    $diff round($diff/7);
        if (
    $diff<4)
            return 
    $diff " week" plural($diff) . " ago";
        return 
    "on " date("F j, Y"strtotime($date));
    }

    // Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
    $stampystamp 1295803279;

    // Display the number of seconds/minutes/hours/days/weeks since the timestamp
    echo getRelativeTime($stampystamp); 
    Untested and rushed, but should work.

  6. #6
    Join Date
    Nov 2008
    Location
    UK
    Posts
    173
    Tokens
    0
    Habbo
    coolchris322

    Latest Awards:

    Default

    Code:
     <?php 
    function howLong($timestamp) { 
        $diff = time()-$timestamp; 
        if ($diff<60) 
            return $diff . " second" . plural($diff) . " ago"; 
        $diff = round($diff/60); 
        if ($diff<60) 
            return $diff . " minute" . plural($diff) . " ago"; 
        $diff = round($diff/60); 
        if ($diff<24) 
            return $diff . " hour" . plural($diff) . " ago"; 
        $diff = round($diff/24); 
        return "on " . date("jS F, Y", $timestamp); 
    }   
    
    howLong("second" + "minutes" + "hours" + "days" + "months" + "years"); //Set a value in each of them. 
    ?>
    


  7. #7
    Join Date
    Jul 2005
    Location
    Leeds
    Posts
    947
    Tokens
    0
    Habbo
    Flumples

    Default

    Quote Originally Posted by Trinity View Post
    That's not what he asked for though, he wants to display the number of days, not seconds/minutes/hours.



    That wouldn't work, it says time() - time().

    I'd do this:
    PHP Code:
    function plural($num) {
        if (
    $num != 1)
            return 
    "s";
    }
     
    function 
    getRelativeTime($date) {
        
    $diff time() - $date;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        if (
    $diff<7)
            return 
    $diff " day" plural($diff) . " ago";
        
    $diff round($diff/7);
        if (
    $diff<4)
            return 
    $diff " week" plural($diff) . " ago";
        return 
    "on " date("F j, Y"strtotime($date));
    }

    // Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
    $stampystamp 1295803279;

    // Display the number of seconds/minutes/hours/days/weeks since the timestamp
    echo getRelativeTime($stampystamp); 
    Untested and rushed, but should work.
    Thanks! Works perfectly

  8. #8
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Quote Originally Posted by LMS16 View Post
    I made this, it basically works abit like facebooks timer (under status' & posts etc..)

    Edit it to how you need it

    PHP Code:
    function howLong($timestamp) {
        
    $diff time()-$timestamp;
        if (
    $diff<60)
            return 
    $diff " second" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<60)
            return 
    $diff " minute" plural($diff) . " ago";
        
    $diff round($diff/60);
        if (
    $diff<24)
            return 
    $diff " hour" plural($diff) . " ago";
        
    $diff round($diff/24);
        return 
    "on " date("jS F, Y"$timestamp);

    Enjoy, Lew.
    I'm sure you did code that. http://snipplr.com/view/4912/relative-time/

  9. #9
    Join Date
    Jun 2008
    Location
    United Kingdom
    Posts
    2,015
    Tokens
    568

    Latest Awards:

    Default

    Quote Originally Posted by BoyBetterKnow View Post
    That's the one I used, I added the link in the first time I wrote the post, but then I had to go back to include HotelUser's post and forgot to add it in.

  10. #10
    Join Date
    Sep 2009
    Location
    Hull
    Posts
    827
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by BoyBetterKnow View Post
    Sorry if you thought I said I made that exact thing, I meant edit

    Lew.
    Im not here to be loved, I love to be hated :-}


Page 1 of 3 123 LastLast

Posting Permissions

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