SkyRanger Posted June 11, 2007 Share Posted June 11, 2007 I am not sure how to even start to tackle this. What I am trying to do is show todays date as today and yesterdays date as yesterday. for example: if ($row['postdate'] = date('Y-m-d') { echo "Today"; } else if { ($row['postdate'] = date('Y-m-(d-1')[not sure of proper code yet still learning]); echo "Yesterday"; } else { echo $row['postdate']; } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 11, 2007 Share Posted June 11, 2007 To get yesterday's date, I would use the strtotime() function: <?php if ($row['postdate'] == date('Y-m-d') // two equal signs for comparison, not one echo "Today"; elseif ($row['postdate'] == date('Y-m-d',strtotime("yesterday")) echo "Yesterday"; else echo $row['postdate']; ?> Ken Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 Awsome Ken, thanks, yeah, as I refreshed this, I was checking out some tutorials on Date, thanks for the help. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 11, 2007 Share Posted June 11, 2007 I had some typo's in the above code, so here is the corrected code: <?php if ($_GET['postdate'] == date('Y-m-d')) // two equal signs for comparison, not one echo "Today"; elseif ($_GET['postdate'] == date('Y-m-d',strtotime("yesterday"))) echo "Yesterday"; else echo $_GET['postdate']; ?> Ken Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 Ok, not sure what I am doing wrong, it is not working for me. Here is what I have: <?php if ($_GET['postdate'] == date('Y-m-d')) echo "Today"; elseif ($_GET['postdate'] == date('Y-m-d',strtotime("yesterday"))) echo "Yesterday"; else $pullbdate = $_GET['postdate']; $postbdate = new DateTime($pullbdate); echo $postbdate->format('M d, Y H:i'); ?> Oops, forgot to mention there is no errors it is defaulting to echo $postbdate->format('M d, Y H:i'); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 11, 2007 Share Posted June 11, 2007 If you have more than one statement in a condition block, you need to put the statements in curly brackets "{ }" <?php if ($_GET['postdate'] == date('Y-m-d')) echo "Today"; elseif ($_GET['postdate'] == date('Y-m-d',strtotime("yesterday"))) echo "Yesterday"; else { $pullbdate = $_GET['postdate']; $postbdate = new DateTime($pullbdate); echo $postbdate->format('M d, Y H:i'); } ?> Ken Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 Ok, I have been trying different things, but grrrr...lol I got rid of the $_GET and replaced it with $pullbdate fixed one problem doing that but it still won't show the Today or Yesterday $pullbdate = $row['postdate']; if ($pullbdate == date('Y-m-d')) echo "Today"; elseif ($pullbdate == date('Y-m-d',strtotime("yesterday"))) echo "Yesterday"; else { $postbdate = new DateTime($pullbdate); echo $postbdate->format('M d, Y H:i'); } It is defaulting to else { $postbdate = new DateTime($pullbdate); echo $postbdate->format('M d, Y H:i'); } Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 Not sure if this matters but in the mysql the postdate = 2007-06-11 08:21:00 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 11, 2007 Share Posted June 11, 2007 Yes, it matters. You are comparing a string with YYYY-MM-DD HH:MM:SS to YYYY-MM-DD, so they will never be the same. Try this instead: <?php pullbdate = date('Y-m-d',strtotime($row['postdate'])); // get the correct format for the comparison if ($pullbdate == date('Y-m-d')) echo "Today"; elseif ($pullbdate == date('Y-m-d',strtotime("yesterday"))) echo "Yesterday"; else { $postbdate = new DateTime($pullbdate); echo $postbdate->format('M d, Y H:i'); }?> Ken Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 Awsome ken, finally got it working, awsome, thanks a bunch. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 Doh, ran into a problem. The code that you gave me works awsome. 90% the way I wanted it too. The problem I ran into is that it removed the time from the posts. Is there a way to post the time along with the Today/Yesterday for example: Today 08:14:00 and Yesterday 15:30:00 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 11, 2007 Share Posted June 11, 2007 Just add the formated time onto the echo statements: <?php pullbdate = date('Y-m-d',strtotime($row['postdate'])); // get the correct format for the comparison if ($pullbdate == date('Y-m-d')) echo "Today " . date('H:i',strtotime($row['postdate'])); elseif ($pullbdate == date('Y-m-d',strtotime("yesterday"))) echo "Yesterday " . date('H:i',strtotime($row['postdate'])); else { $postbdate = new DateTime($pullbdate); echo $postbdate->format('M d, Y H:i'); }?> Ken Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted June 11, 2007 Author Share Posted June 11, 2007 bowing down gracefully, thanks ken, you are a life saver 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.