Make And Enquiry

dateToString() PHP Function

24th April 2009

We've recently had a project where a calendar was central to the build.  We wanted it be really simple and intuitive to use, so we decided to represent dates in both a traditional format and also in a more conversational style such as 'Yesterday' or 'In 2 weeks'. 

 

This function works by taking a time stamp in the format yyyy-mm-dd HH:MM:SS.  Feel free to use and change the code below, but if you improve it or fix any bugs, please let us know! 

 

One thing we would say is that whilst every effort has been made to ensure it is bug free, we can't take any responsibility for any loss, financial or otherwise, as a result of using our code.  However, that said - enjoy.

 

class Tools
{
    /**
     * This function turns a date into a string such as tomorrow, yesterday, next week, next month etc
     * @param $date
     * @return unknown_type
     */
    public static function dateToString($date)
    {
        //Change this date into a timestamp
        $time = strtotime($date);
       
        //Today's date
        $today = time();
       
        //Now lets get the year to see if it's the same year
        $today_y = date('Y', $today);
        $time_y = date('Y', $time);
       
        if($today_y == $time_y)
        {
            //Same year so we zoom in further
           
            //Get the day and the month
            $today_dm = date('j|n', $today);
            $time_dm = date('j|n', $time);
           
            $today_dm = explode('|', $today_dm);
            $time_dm = explode('|', $time_dm);
           
            //See if we are the same month
            if($today_dm[1] == $time_dm[1])
            {
                //Same month
               
                //Get the number of weeks into the year each date is
                $today_w = date('W', $today);
                $time_w = date('W', $time);
               
                if($today_w == $time_w)
                {
                    //Same Week
                    //Work out how many days away we are
                    if($today_dm[0] == $time_dm[0])
                    {
                        return "Today";
                    }else{
                        if($today_dm[0] > $time_dm[0])
                        {
                            //Past
                            $difference = $today_dm[0] - $time_dm[0];
                           
                            switch($difference)
                            {
                                case 1:
                                    return "Yesterday";
                                    break;
                                default:
                                    return $difference.' days ago';
                                    break;
                            }
                        }else{
                            //Future
                            $difference = $time_dm[0] - $today_dm[0];
                           
                            switch($difference)
                            {
                                case 1:
                                    return "Tomorrow";
                                    break;
                                default:
                                    return 'In '.$difference.' days';
                                    break;
                            }
                        }
                    }
                }else{
                    //Different Week
                    if($today_w > $time_w)
                    {
                        //Past
                        $difference = $today_w - $time_w;
                       
                        switch($difference)
                        {
                            case 1:
                                return "Last week";
                                break;
                            default:
                                return $difference.' weeks ago';
                                break;
                        }
                    }else{
                        //Future
                        $difference =  $time_w - $today_w;
                       
                        switch($difference)
                        {
                            case 1:
                                return "Next week";
                                break;
                            default:
                                return 'In '.$difference.' weeks';
                                break;
                        }
                    }
                }
               
            }else{
                //Different month
                if($today_dm[1] > $time_dm[1])
                {
                    //Previous
                    $difference = $today_dm[1] - $time_dm[1];
                   
                    switch($difference)
                    {
                        case 1:
                            return 'Last month';
                            break;
                        default:
                            return $difference.' months ago';
                            break;
                    }
                }else{
                    //Future
                    $difference = $time_dm[1] - $today_dm[1];
                   
                    switch($difference)
                    {
                        case 1:
                            return 'Next month';
                            break;
                        default:
                            return 'In '.$difference.' months';
                            break;
                    }
                }
            }
           
        }else{
            //Now this year, so let's work out if it's in the past or the future
            if($today_y > $time_y)
            {
                //Previous
                $difference = $today_y - $time_y;
               
                switch($difference)
                {
                    case 1:
                        return 'Last year';
                        break;
                    default:
                        return $difference.' years ago';
                        break;
                }
            }else{
                //Future
                $difference = $time_y - $today_y;
               
                switch($difference)
                {
                    case 1:
                        return 'Next year';
                        break;
                    default:
                        return 'In '.$difference.' years';
                        break;
                }
            }
        }
    }
}



Comments to “dateToString() PHP Function”

18th June 2009 15:20
Yuan says:
I coded a similar function last year that I thought I'd share with you ( http://www.tjc-oxford.com/admin/functions/time.txt )

I notice however that if today's date were the 30th of June, your function will report "next month" for a target date of 1st of July. Whether this kind of behaviour is desired or not depends on how it is used.

For example, in the application that I coded for, the reported "fuzzy" date was used as a deadline (hence why imminent dates are coloured for clarity), and so the 1st of July would be reported as "tomorrow" on the 30th of June, while "next month" will always be around 30 days.

There's also a function for calculating the date (timestamp) of a given number of weekdays in the future, which you might find useful, it's not very efficient since I've used an iterative scheme (didn't have time to work out the exact algorithm behind it), but feel free to use.

I'm not in direct control of the hosting any more, so if the page disappears, and you still want to get at the script, email me

18th June 2009 16:29
Yuan says:
I'd also like to let you know, from experience, unless you are certain that you will pass a valid date to strtotime(), you may find that for an ill-formatted $date, strtotime() will return zero, resulting in the function thinking it is January 1st 1970.
A further problem may also be related to the 32bit time overflow for future dates, although I haven't tried to see what happens.

It would be prudent to add an exception for these cases, since the function reporting "39 years ago" may be misleading


Leave a Reply
 





  • captcha