Recently, I have to output the age of a date in words and didn't have a framework to work with. The first steps were to parse the given date into an array and create a timestamp with the individual parts. The difference between the timestamp of now and the created timestamp yielded the age in seconds. The result can be used to compare with seconds of a day, week, etc.
The following snippet outputs the age of a date in day, if the given date is less than seven days, in weeks if it's less than four weeks an so on.
$date_parsed = date_parse($date);
$date_timestamp = mktime(0,0,0, $date_parsed['month'], $date_parsed['day'], $date_parsed['year']);
$now_timestamp = time();
$timestamp_diff = $now_timestamp - $date_timestamp;
if ($timestamp_diff < (60*60*24*7)) {
return floor($timestamp_diff/60/60/24)." Days";
} elseif ($timestamp_diff < (60*60*24*7*4)) {
return floor($timestamp_diff/60/60/24/7)." Weeks";
} else {
return floor($timestamp_diff/60/60/24/30)." Months";
}
I came across a handy piece of code to print out the “age of a date” in words on ThinkPHP. This code should print out the difference between two dates in words (X Days/Months/Weeks/Years). I have modified the original piece of code to prin...
Tracked: Apr 13, 08:07