Jump to content

[SOLVED] Today / Yesterday


SkyRanger

Recommended Posts

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'];
}

Link to comment
https://forums.phpfreaks.com/topic/55092-solved-today-yesterday/
Share on other sites

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

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

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

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

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

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

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

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

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.