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;
}
}
}
}
}





Pure Innovation Ltd