Hi there,
I am writing a site that contains articles that get posted from time to time by different authors. I need to have articles posted in the past 7 days bolded.
I have the presentation logic down - it is actually very easy. But, I am not familiar enough to with UNIX timestamps to know what the the most acurate way to compare them.
Right now, whenevr a post is made, I use the time() function to send a timestamp into the databse. It gets formateed with date() when the article is accessed. In order to compare the current date with the last date, should I be using time() again, and compare that to the stamp in the database? If so, how exactly should that comparison get played out; if not, is there a better method I should be using?
Thanks in advance. I'll be gone for a few hours, but I'll be back later on in the day to discuss this further.
{ d }
Comparing UNIX Timestamps
Moderator: General Moderators
- URWhatUXpress
- Forum Newbie
- Posts: 11
- Joined: Sat Aug 30, 2003 5:00 pm
- Location: Grand Rapids, MI
A UNIX timestamp differs from a MySQL timestamp. UNIX gives the number of seconds that have past since January 1 1970 00:00:00 GMT,
and MySQL timestamps are generally in a YYYYMMDDHHMMSS format.
To find the difference between two UNIX timestamps to simply to something like this...
and MySQL timestamps are generally in a YYYYMMDDHHMMSS format.
To find the difference between two UNIX timestamps to simply to something like this...
Code: Select all
$timeDif = time() - $timePosted; // $timePosted will be the UNIX timestamp of when someone posted a message etc
$daysSincePost = $timeDif / 864000; // 864000 is the number of seconds in one day.
if($daysSincePost > 7)
{
echo "This post is more than a week old.";
}
else
{
echo "This post is less than a week old.";
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK