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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.