General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 31st, 2003, 12:00 PM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Compare timeStamps

I would like to compare two timestamps that are in a MySQL db.
The reason I have 2 timestamps, is because one updates to NOW when the page is loaded.

Before the timeStamp goes threw $dateFormat, it looks like this, if that helps any. 20030731112209

The function that I used doesn't seem to work correctly.

Tables as follows:
articleDate timestamp(14) NOT NULL,
timestampNow timestamp(14) NOT NULL,


PHP Code:
//Compare Function
function thatDate($time){ 
$time=time()-$time

$weeks=$time/604800
$days=($time%604800)/86400
$hours=(($time%604800)%86400)/3600
$minutes=((($time%604800)%86400)%3600)/60
$seconds=(((($time%604800)%86400)%3600)%60); 

$timestring=' '
if(
round($days)) $timestring.=round($days)."d "
if(
round($hours)) $timestring.=round($hours)."h "
if(
round($minutes)) $timestring.=round($minutes)."m"
if(!
round($minutes)&&!round($hours)&&!round($days)) $timestring.=" ".round($seconds)."s"
$timestring.=''

return 
$timestring


// Query the db
$query "SELECT 
date_format(articleDate, '$dateFormat') as articleDate,
date_format(timestampNow, '$dateFormat') as timestampNow FROM $tblName WHERE id = '$id'"
;
$result mysql_query($query) or die ("Error in query: $query. " mysql_error());
$row mysql_fetch_object($result);
if (
$row)



Page outputs:
Now: <? echo $row->timestampNow; ?><br>
Date Posted: <? echo $row->articleDate; ?><br>
Article is <? echo thatDate(('articleDate')); ?> old.

The results that I'm getting.
Now: Thursday July 31st, 2003 - 12:58:48 PM
Date Posted: Thursday July 31st, 2003 - 12:53:17 PM
Article is 1d 20h 59m old. <-- Wrong

Reply With Quote
  #2  
Old July 31st, 2003, 01:20 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Consider using int(11) as your time field and inserting time() from PHP. Then you can use FROM_UNIXTIME() and DATE_FORMAT(FROM_UNIXTIME(),'format') to extract readable dates while also making the math easy. Note that the timestamp field prepends the year, month, and day to the seconds string, so any math you do on that unconverted number is going to come out pretty funky.

Reply With Quote
  #3  
Old July 31st, 2003, 04:06 PM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Thanks dhouston,
I wil convert my times, and try again.

If I still seem to have problems, I will post back, thanks again

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Compare timeStamps


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT