TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-10-2009, 06:49 PM   #1 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default php time?

So I have a video id and at timestamp stored in a database. I'm created a cron.php to pull the data in a while() loop and compare the time w/ the current time. If it's been 8hrs or more, then the row is to be deleted, if not, then nothing happens. In theory it makes sense, but i'm unsure how to achieve the time math. Here what I'm working with so far:

Code:
$sql = "SELECT * FROM credits_temp";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) {

	//Database time
	$dataBaseTime = $row['time'];
	$curTime	= '2009-06-09 20:54:13';//not sure how to use date for this time format...

	$difference = "i have no idea"

	if( $difference >= "8hrs somehow")
	{
		//delete the row
	}



}
Any help or guidance would be most helpful. Thanks :D

Dave
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:01 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Is your database storing these dates as a string? What field type is the date stored in?

PHP generally uses whats called a Unix Timestamp to do time. A Unix Timestamp is the number of seconds from 1/1/1970 (aka the Unix Epoch). This provides easy means to get the date and difference them.

To get the current date, use:
PHP Code:
time() 
Use these various method to get differences
PHP Code:
$minute 60
$hour $minute*60;
$day $hour*24;
$year $day*365;

//Eight hours from now
$8hrFromNow time()+($hour*8);

//Eight hours ago (the rows timestamp must be equal or lower than this number)
$8hrAgo time()-($hour*8); 
To format these timestamps, use Date

The reason I bring this up is because the DB might be spitting out a formatted timestamp at you. If you can give us the column type I can tell you how to work with it.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 07:12 PM   #3 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

Thanks for the quick response. The database is set to automatically set the time/date when the entry is made (CURRENT_TIMESTAMP). So the format is set like so:

2009-06-09 20:54:13

So yes I pull it as a string,

$row['time'] displays "2009-06-09 20:54:13"

So i need to get the current time in that same format I would assume, then find the difference of the 2. If it's 8 or more, perform action.
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:18 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by DizzyD View Post
Thanks for the quick response. The database is set to automatically set the time/date when the entry is made (CURRENT_TIMESTAMP). So the format is set like so:

2009-06-09 20:54:13

So yes I pull it as a string,

$row['time'] displays "2009-06-09 20:54:13"

So i need to get the current time in that same format I would assume, then find the difference of the 2. If it's 8 or more, perform action.
What data type is it? I would assume timestamp if that is the default value. If that is corrent, that means that you are getting the a formatted timestamp opposed to a string. Have a query something like this
Code:
SELECT * FROM db WHERE DATESUB(CURDATE(),INTERVAL 8 HOURS)>=insert_time
Anything that query returns will be more than eight hours old. insert_time being the column name of your timestamp. This code is not tested, but should work.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
DizzyD (06-10-2009)
Old 06-10-2009, 07:19 PM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Above code fixed.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 07:27 PM   #6 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

So if i'm reading this correctly, this should only pull rows that are 8+ hours?
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:29 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

That is correct.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 07:30 PM   #8 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

and insert_time is that the table name? As in my case it would just be "time"?
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:31 PM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by DizzyD View Post
and insert_time is that the table name? As in my case it would just be "time"?
insert_time is whatever you named the column storing the timestamp. DB is poorly named, it is the table (I don't know why I put db as the name).
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 07:33 PM   #10 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

Wonderful! Thanks so much for the help! If I run into problems, i'm sure i'll be back. Wish me luck!

Thanks again!
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:35 PM   #11 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Always glad to help.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 07:38 PM   #12 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

Well I knew it wouldn't be long. I'm getting "supplied argument is not a valid MySQL result resource" errors and i'm assuming it is because of the sql statement. Is it a caps lock issue or something? I have no idea..

Here's what i'm working with:

Code:
$sql = "SELECT * FROM credits_temp WHERE DATESUB(CURDATE(),INTERVAL 8 HOURS)>=time";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) {

echo $row['time'];
echo "<br />";

}
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:47 PM   #13 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by DizzyD View Post
Well I knew it wouldn't be long. I'm getting "supplied argument is not a valid MySQL result resource" errors and i'm assuming it is because of the sql statement. Is it a caps lock issue or something? I have no idea..

Here's what i'm working with:

Code:
$sql = "SELECT * FROM credits_temp WHERE DATESUB(CURDATE(),INTERVAL 8 HOURS)>=time";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) {

echo $row['time'];
echo "<br />";

}
Case has no bearing in an SQL query unless you are searching for a case-sensitive value. It is general practice to caps SQL commands and keywords.

Run this:
Code:
$sql = "SELECT * FROM credits_temp WHERE DATESUB(CURDATE(),INTERVAL 8 HOURS)>=time";
$query = mysql_query($sql);

$row = mysql_fetch_array($query) or die(mysql_error());
This will output the error returned.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 07:55 PM   #14 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

bah, I guess I should of done that from the start anyway :P

This is the error it spit out:
Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/dave/public_html/dizzyd/dev1/cron.php on line 21
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HOURS)>=time' at line 1
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 07:58 PM   #15 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Change HOURS to HOUR
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 08:01 PM   #16 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

Hmm... Still no dice. Exact same error.
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 08:11 PM   #17 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

What is the data type of time? I still haven't gotten a clear answer to that.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 08:12 PM   #18 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

The command is not DATESUB, its DATE_SUB. I can't believe I missed that, I'm a little rusty on my MySql.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 08:18 PM   #19 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

I apologize for that, here is a screenshot of the db in phpmyadmin:
DizzyD is offline  
Reply With Quote
Old 06-10-2009, 08:21 PM   #20 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Default

Ahhh, good timing... just after a bit of research, I came across that as well. Thanks again for your help :D
DizzyD is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
Tutorial for Beginners - Not a usual tutorial [Read it all and i think it will help] codefreek Absolute Beginners 8 01-26-2009 03:14 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
$36,000 - Full Time PHP Coder / Designer Position Rex The Lounge 10 01-18-2008 08:32 PM
Calculating the Execution Time Wildhoney Tips & Tricks 0 09-14-2007 11:14 PM


All times are GMT. The time now is 04:07 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design