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);<br />$date_timestamp = mktime(0,0,0, $date_parsed['month'], $date_parsed['day'], $date_parsed['year']);<br /><br />$now_timestamp = time();<br />$timestamp_diff = $now_timestamp - $date_timestamp;<br /><br />if ($timestamp_diff < (60*60*24*7)) {<br /> return floor($timestamp_diff/60/60/24)." Days";<br />} elseif ($timestamp_diff < (60*60*24*7*4)) {<br /> return floor($timestamp_diff/60/60/24/7)." Weeks";<br />} else {<br /> return floor($timestamp_diff/60/60/24/30)." Months";<br />}
Schreibe einen Kommentar