Date display as "yesterday" or "tomorrow"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Date display as "yesterday" or "tomorrow"

Post by edawson003 »

I want to display a mysql records entry date as either "today", "yesterday" or the actual date (i.e. 10/7/2009). See below attempt at condition for each option. For some reason, I keep getting the actual date as display return, eventhough I change the test record's entry date from today's date, yesterday's date and the day before yesterday. Ive been tweaking and adjust code all evening with no luck. Any thoughts on how I may accomplish this? Don't me tell there's a two liner piece of code out there that does this already?

:banghead:

Code: Select all

 
<?
ini_set('date.timezone', 'America/Los_Angeles');
?>
<?
                        $enteredby =  $row['acctid'];                       
                        $newweightquery = "SELECT entrydt FROM table WHERE enteredby = $enteredby ORDER BY entrydt DESC LIMIT 1,1"; 
                        $newresult = mysql_query($newweightquery);
                        while($row = mysql_fetch_array($newresult)){
                        $dtdisplay = $row['entrydt'];                           
                        }                       
                        $todaysdt = date('Y-m-d',time());                       
                        $yesterdaysdt = date('Y-m-d',(strtotime('-1 day', strtotime($todaysdt))));
                        echo "today's date: $todaysdt <br>";    
                        echo "yesterday's date: $yesterdaysdt <br>";    
                        echo "Entry Dt of record in database: $dtdisplay <br>";                     
                                            
                        if($todaysdt == $dtdisplay){ 
                        echo "today";
                         }
                        elseif($yesterdaysdt == $dtdisplay){
                        echo "yesterday";
                         }
                        elseif($todaysdt <> $dtdisplay && $yesterdaysdt <> $dtdisplay){
                        echo date('m-d-Y',strtotime($dtdisplay));
                          }
                          
                         ?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Date display as "yesterday" or "tomorrow"

Post by requinix »

Is entrydt a DATE or DATETIME?
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Date display as "yesterday" or "tomorrow"

Post by edawson003 »

It is a datetime. Awww, I just added this

Code: Select all

 
$dtdisplay = date('Y-m-d',(strtotime($dtdisplay)));
and now it works. Thanx!
Post Reply