SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Friendly dates

  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Friendly dates

    I've got a bunch of MySQL rows which contain Unix timestamps. I want to output the dates in a 'friendly' way, i.e. "2 hours ago" or "24 minutes ago" as opposed to "12th January 2008".

    I've seen a couple of tutorials knocking around which provide functions that accept a timestamp as a parameter and return the necessary string, but I've got to be fairly performance-conscious here. While this code (in the second post down) looks like it could easily be adapted to fit my needs, I'm a bit concerned because it's likely I'll have to call this function upwards of 100 times on a single page, and the volume of page requests is likely to be fairly high.

    Does anyone know of a more lightweight method for performing this task? Or would the code I linked to above not cause any real performance issues? I'm a bit of a newbie when it comes to script performance I'm afraid!
    Sam Hastings

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's horrible code...

    I'll share ya some way better code once I get home. PM me your email address. I'll email it to you tonight.

    Jeff

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    aw, come on, jeff, share it with the rest of us too, please
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    England
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $then 
    '1219258800';
    $now time();
    $dif $now $then;

    if (
    $dif 24*3600) {
        
    $string floor($dif/(24*3600)) ." days ago";
    }elseif(
    $dif 3600){
        
    $string floor($dif/3600) ." hours ago";
    }elseif(
    $dif 60){
        
    $string floor($dif/60) ." minutes ago";
    }

    echo 
    $string;

    ?>
    Try that
    Last edited by 01706; Aug 20, 2008 at 12:38. Reason: Changed code


Bookmarks

Posting Permissions

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