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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.