jason white
jason white

Reputation: 711

PHP Date or TimeStamp Function to check time elapsed within the same day, same week, or more than a week

For PHP, the function that gives the date of the year.

   $date = date('n/j/Y', $time);
  echo $date; 

or express as timestamp

   $time = strtotime($date);
   echo $time; 

Basically I want to have my postings list month/day/yr when it's more than a week old. and listed as the monday, tuesday ...day of the week when the postings are within the week. And when the posting is within the same day, then express how many hrs ago or how many minutes ago.

Do I need to use timestamp to do subtraction to see the time elapsed? And it does how do I bring it back such a way as what's time elapsed within the same day or within the same week?

thx

Upvotes: 0

Views: 273

Answers (1)

KingCrunch
KingCrunch

Reputation: 132061

$currentTime = time();
if ($currentTime < strtotime('1 year ago', $currentTime) {
  echo "at least a year old";
} else if ($currentTime < strtotime('1 month ago', $currentTime) {
  echo "at least a month old (but not a year)";
} else if ($currentTime < strtotime('1 week ago', $currentTime) {
  echo "at least a week old (but not a month)";
} else if ($currentTime < strtotime('1 day ago', $currentTime) {
  echo "at least a day old (but not a week)";
}

Upvotes: 2

Related Questions