Page 1 of 1

Comparing UNIX Timestamps

Posted: Sun Nov 02, 2003 10:54 am
by URWhatUXpress
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 }

Posted: Sun Nov 02, 2003 1:19 pm
by Gen-ik
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...

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.";
}

Posted: Sun Nov 02, 2003 7:12 pm
by m3mn0n
[mysql_man]date[/mysql_man]

Posted: Mon Nov 03, 2003 3:57 am
by twigletmac
Why not use MySQL's date and time functions. Then you can test the date in your SELECT statement as well as format it. (Of course this assumes that you are using MySQL but most DB's have similar functionality).

Mac