Jump to content

Recommended Posts

Ok, so here's the problem. Well, not really a problem, just a lack of information. You know on forums (such as the one PHPFreaks is using [sMF]) how does it list the last post in that thread and it changes it depending on well, the last post made in that thread and the current date. What I am looking for is information on how to do this.

 

How would I save the date of a post in an appropriate format, and how would I display whether the last post was "Today", "Yesterday", or if the last post was older then two days, simple display the date of the last post in a simple format like mm-dd-yyyy.

 

Any help is greatly appreciated.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/
Share on other sites

UPDATE:

Administrator(s) really need to change the forum setting that forbids you from editing posts...

 

Here is some random code I threw together off the top of my head. As this is not something I am working on personally, I did not nor do I feel like testing this code:

 

<?php
//database configuration
//open database

$cur_dd = date("j"); //returns the day in 1 through 31 format
$cur_mm = date("F"); //returns the month in January through December format
$cur_yy = date("Y"); //returns the day in 4 digits (eg: 2007)

$query = "INSERT INTO POSTS_TABLE (day, month, year) VALUES ('$cur_dd', '$cur_mm', '$cur_yy')";
mysql_query($query) or die('Error, insert query failed');
}

//close database

?>

 

...assuming a URL would be structured like:

 

mydomain.com/blah.php?pid=1234

 

<?php
$pid = $_GET['pid'];

$cur_dd = date("j");
$cur_mm = date("F");
$cur_yy = date("Y");

$sql = "SELECT * FROM POSTS_TABLE WHERE id = '$pid'";
$result = mysql_query($sql, $conn) or die(mysql_error());

while ($call = mysql_fetch_array($result)) {

$post_dd = $call['day'];
$post_mm = $call['month'];
$post_yy = $call['year'];

}

if ($post_dd == $cur_dd) {

$dsp_date = "Today";

} elseif ($post_dd < $cur_dd && $post_dd > $cur_dd++) {

$dsp_date = "Yesterday";

} else {

$dsp_date = "$post_mm $post_dd, $post_yy";

}

$showdate = "Last Post by $last_poster on $dsp_date at $dsp_time";

//$last_poster and $dsp_time are unassigned variables
//didnt do that part

?>

 

...call the 'function'

 

<div id="date">
<?php echo "$showdate"; ?>
</div>

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-421592
Share on other sites

Ok, so through your -->LINK<-- I have managed to understand how to get a UNIX timestamp (which is what i should be using).

 

Next question, is my PHP code (aside from not using a UNIX timestamp) following the right idea? Moreover, how exactly would I go about doing that and also taking into consideration time? How would I determine/format the time in a 12-hour format, not 24?

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-421598
Share on other sites

Create a field called `post_date` or whatever you want. Make it an INT(11), NOT NULL, and create an index for it.

 

The insert:

 

<?php
//database configuration
//open database

$query = "INSERT INTO `POSTS_TABLE` (`post_date`) VALUES (time())";
mysql_query($query) or die('Error, insert query failed');

//close database

?>

 

Now you have a Unix Timestamp stored in `post_date`.

 

To get last post:

 

<?php
$sql = "SELECT `post_id`, `post_title`, `post_date` FROM `POSTS_TABLE`
        ORDER BY `post_date`
        LIMIT 0,1
       ";
$result = mysql_query($sql, $conn) or die(mysql_error());
list ($post_id, $post_title, $post_date) = mysql_fetch_assoc($result);

// format as you wish - example:

$content = 'Last post on: ' . date("M jS, Y \a\t g:i A T", $post_date);
$content .= '<br><i><a href="viewforum.php?pid=' . $post_id . ">$post_title</a></i>';

echo $content;

?>

 

PhREEEk

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-421607
Share on other sites

Sorry, I meant, I know how to do that, umm; let me try and explain this in a different manner.

 

examplegq4.gif

 

What I am asking, is essentially how to determine whether to show the post as being "Today", "Yesterday", or the date of the last post. I understand how to create the timestamp, what I do not understand is how to determine whether to show today, yesterday or the date of the last post depending upon both the current time and date compared to the last posts time and date.

 

So for example:

 

if ($last_post == $current_date) {
echo "Last post TODAY by USER at 01:30 AM"
}
elseif ($last_post == $current_date--) {
echo "Last post YESTERDAY by USER at 07:00 PM"
}
else {
echo "Last post DECEMBER 25, 2007 by USER at 02:00 PM"
}

 

... wait. Did i just answer my own question ???

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-421621
Share on other sites

Thank you PhREEEk.

 

Ok, this is what, (if im understanding this correctly), the code should look like: I am not too sure if it will work though.

 

NEWPOST.PHP

<?php
//database configuration
//open connection

$query = "INSERT INTO POST_TABLE (post_date) VALUES (time())";
mysql_query($query) or die('Error, insert query failed');
}

//close connection
?>

 

SHOWPOST.PHP

<?php
$pid = $_GET['pid'];

//database configuration
//open connection

$sql = "SELECT post_date FROM POST_TABLE WHERE id = '$pid'";
$result = mysql_query($sql, $conn) or die(mysql_error());
list ($post_date) = mysql_fetch_assoc($result);

$last_dd = date("j", $post_date);
$cur_dd = date("j");

if ($last_dd == $cur_dd) {
$showdate = 'Last post Today ' . date("\a\t g:i A T", $post_date);
} elseif ($last_dd == $cur_dd--) {
$showdate = 'Last post Yesterday ' . date("\a\t g:i A T", $post_date);
} else {
$showdate = 'Last post on ' . date("M jS, Y \a\t g:i A T", $post_date);
}

echo $showdate;

//close connection
?>

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-421628
Share on other sites

I don't know where $pid is coming into the script from, or rather why...

 

You just need to pull the latest post before anything else... so:

 

<?php
$sql = "SELECT `post_author`, `post_date` FROM `POSTS_TABLE`
        ORDER BY `post_date`
        LIMIT 0,1
       ";
$result = mysql_query($sql, $conn) or die(mysql_error());
list ($post_author, $post_date) = mysql_fetch_assoc($result);

 

By your example, you don't want the post title, you want the date and author. So the above will get you that. If you're doing your posts table with a poster_id instead of full handle for author, you would do a JOIN with the users table using the poster_id as a foreign key to get their handle. At any rate, we now have the author's handle and the post date for the most recent post at the time of our query... all good. Now we are going to use mktime() and date() to compare and format respectively.

 

<?php
// is this post time > than today at midnight?
if ( $post_time > mktime(0, 0, 0) ) {
    $showdate = '<b>Today</b> ' . date("\a\t h:i A T", $post_date);
// if not, then is it greater than yesterday at midnight?
} elseif ( $post_time > mktime(0, 0, 0, date("m"), date("d")-1) ) {
    $showdate = '<b>Yesterday</b> ' . date("\a\t h:i A T", $post_date);
// if neither of those, than it must be older
} else {
    $showdate = date("M jS, Y \a\t h:i A T", $post_date);
}
// add poster handle
$showdate .= '<br>by <a href="profile.php?name="' . $post_author . '">$post_author</a>';

echo $showdate;

 

That code would produce what you imaged above...

 

PhREEEk

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-421683
Share on other sites

  • 4 weeks later...

I would just suggest some simple maths. Check the years are equal. Then check the months are equal. Then take today's day value from the post's date's value. If the result is zero then you know the post was done today. If the result is 1 then you know the post was done yesterday.

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-444022
Share on other sites

EDIT:

 

(it wouldn't let me edit my last post)

 

The date formatting doesn't seem to work as intended. It does not say at.

 

For example, what that is suppose to show is:

 

Jan 00th, 2008 at 00:00 PM CST

 

However, the t is not showing up for me. It only looks like:

 

Jan 00th, 2008 a 00:00 PM CST

 

    $showdate = date("M jS, Y \a\t h:i A T", $post_date);

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-444026
Share on other sites

Uhm...

echo date('M jS, Y', $post_date) . ' at ' . date('h:i A T', $post_date);

I guess sometimes you're concentrating on something so much that you miss the obvious.

 

I never missed anything. I would just prefer to write the output string in such as manner that wouldn't involve me appending something before and after the at text. Adding another slash before the t seems to have solved it. I think it has something to do with where the t was situated. /shrug

 

$showdate = date("M jS, Y \a\\t h:i A T", $post_date);

 

I would just suggest some simple maths. Check the years are equal. Then check the months are equal. Then take today's day value from the post's date's value. If the result is zero then you know the post was done today. If the result is 1 then you know the post was done yesterday.

 

Your idea seems like it would involve unnecessary coding. Not to mention, PHP_PhREEEk's code pretty much does exactly what you just described.

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-444039
Share on other sites

Quite possibly. Im not sure what you mean by the tab character, but either way, problem solved on both accounts now. I think phreeek just made a typo.

The tab character is what appears when you press the Tab key in text editors (unless they replace the tab with a number of spaces). It's what I use to indent my code...

 

Anyway, when you use "\t" in PHP it's converted to a tab character in much the same way as "\n" is converted into a newline character. Phreeek had used date("... \a\t ..."); since it was in double quotes and not single quotes, the \t was converted into a tab character. Adding another slash before it escaped the original backslash so you were effectively left with the two characters '\t' and not the tab character. So, if you were to use single quotes instead of double quotes then the extra backslash wouldn't be necessary.

 

Either way I'm glad you found a solution to your problem.

Link to comment
https://forums.phpfreaks.com/topic/82894-solved-relative-date/#findComment-444048
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.