Jump to content

[SOLVED] Showing dates like "yesterday", "1 week ago" etc.


therealwesfoster

Recommended Posts

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

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

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

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

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

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.