therealwesfoster Posted April 25, 2008 Share Posted April 25, 2008 I'm needing to know a simple way to go about showing dates like "2 days ago", "today" etc. (instead of showing a regular date like 03/13/1990) I know how to do this using many IF statements, but I'm wanting a simpler way of doing this. The date statuses should be the following: “In 1 Month” “In 1 Week” “In 2 Days” “Tomorrow” “Today” “2 Days Ago” “A Week Ago” “2 Weeks Ago” “1 Month Ago” Any links, scripts or comments are appreciated. Wes Quote Link to comment Share on other sites More sharing options...
DBookatay Posted April 25, 2008 Share Posted April 25, 2008 Tomorrow is: $tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("Y")); $tomorrow = date("m/d/Y", $tomorrow); Yesterday is: $yesterday = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")); $yesterday = date("m/d/Y", $yesterday); Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted April 25, 2008 Author Share Posted April 25, 2008 Thanks for the reply, but I think you misunderstood my question. Heres a better explanation: Usually a user will see Due Date: April 26, 2008. I want it to show instead Due Date: Tomorrow. And I want it to change for the following: In 1 Month” “In 1 Week” “In 2 Days” “Tomorrow” “Today” “2 Days Ago” “A Week Ago” “2 Weeks Ago” “1 Month Ago” Now if the date is something that isn't in that list (example: 2 months ago) then I want it to just show the date like normal. Thanks Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted April 25, 2008 Share Posted April 25, 2008 he didnt misunderstand your question...if you do it his way just run an if statement. if($date=$tomorrow) { echo 'Due Date: Tomorrow'; } else { echo 'Due Date: '.$date; } Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted April 25, 2008 Author Share Posted April 25, 2008 he didnt misunderstand your question...if you do it his way just run an if statement. if($date=$tomorrow) { echo 'Due Date: Tomorrow'; } else { echo 'Due Date: '.$date; } Is there a simpler way of doing this? Thats how I planned on doing it in the first place, but I was just wanting to know if there was a simpler way of doing it. Thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 25, 2008 Share Posted April 25, 2008 Set up an array like this: <?php $x = array("In 1 Month","In 1 Week", "In 2 Days", "Tomorrow", "Today", "2 Days Ago", "A Week Ago", "2 Weeks Ago", "1 Month Ago"); $ta = array(); foreach($x as $str) $ta[date('Y-m-d',strtotime(str_replace('In ','+',$str)))] = $str; echo '<pre>' . print_r($ta,true) . '</pre>'; ?> The in your code, you can do something like this: <?php $date = '4/27/2008'; $phrase = (array_key_exists(date('Y-m-d',strtotime($date)),$ta))?$ta[date('Y-m-d',strtotime($date))]:date('F jS, Y'); echo $phrase; ?> Ken Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted April 25, 2008 Author Share Posted April 25, 2008 Set up an array like this: <?php $x = array("In 1 Month","In 1 Week", "In 2 Days", "Tomorrow", "Today", "2 Days Ago", "A Week Ago", "2 Weeks Ago", "1 Month Ago"); $ta = array(); foreach($x as $str) $ta[date('Y-m-d',strtotime(str_replace('In ','+',$str)))] = $str; echo '<pre>' . print_r($ta,true) . '</pre>'; ?> The in your code, you can do something like this: <?php $date = '4/27/2008'; $phrase = (array_key_exists(date('Y-m-d',strtotime($date)),$ta))?$ta[date('Y-m-d',strtotime($date))]:date('F jS, Y'); echo $phrase; ?> Ken Super nice of you. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.