Comparing UNIX Timestamps

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
URWhatUXpress
Forum Newbie
Posts: 11
Joined: Sat Aug 30, 2003 5:00 pm
Location: Grand Rapids, MI

Comparing UNIX Timestamps

Post 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 }
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.";
}
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[mysql_man]date[/mysql_man]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

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