 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
06-10-2009, 06:49 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
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
|
|
|
|
06-10-2009, 07:01 PM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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:
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.
|
|
|
|
06-10-2009, 07:12 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
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.
|
|
|
|
06-10-2009, 07:18 PM
|
#4 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by DizzyD
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.
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
06-10-2009, 07:19 PM
|
#5 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Above code fixed.
|
|
|
|
06-10-2009, 07:27 PM
|
#6 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
So if i'm reading this correctly, this should only pull rows that are 8+ hours?
|
|
|
|
06-10-2009, 07:29 PM
|
#7 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
That is correct.
|
|
|
|
06-10-2009, 07:30 PM
|
#8 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
and insert_time is that the table name? As in my case it would just be "time"?
|
|
|
|
06-10-2009, 07:31 PM
|
#9 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by DizzyD
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).
|
|
|
|
06-10-2009, 07:33 PM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
Wonderful! Thanks so much for the help! If I run into problems, i'm sure i'll be back. Wish me luck!
Thanks again!
|
|
|
|
06-10-2009, 07:35 PM
|
#11 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Always glad to help.
|
|
|
|
06-10-2009, 07:38 PM
|
#12 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
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 />";
}
|
|
|
|
06-10-2009, 07:47 PM
|
#13 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by DizzyD
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.
|
|
|
|
06-10-2009, 07:55 PM
|
#14 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
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
|
|
|
|
|
06-10-2009, 07:58 PM
|
#15 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Change HOURS to HOUR
|
|
|
|
06-10-2009, 08:01 PM
|
#16 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
Hmm... Still no dice. Exact same error.
|
|
|
|
06-10-2009, 08:11 PM
|
#17 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
What is the data type of time? I still haven't gotten a clear answer to that.
|
|
|
|
06-10-2009, 08:12 PM
|
#18 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
The command is not DATESUB, its DATE_SUB. I can't believe I missed that, I'm a little rusty on my MySql.
|
|
|
|
06-10-2009, 08:18 PM
|
#19 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
I apologize for that, here is a screenshot of the db in phpmyadmin:

|
|
|
|
06-10-2009, 08:21 PM
|
#20 (permalink)
|
|
The Wanderer
Join Date: Feb 2009
Posts: 11
Thanks: 1
|
Ahhh, good timing... just after a bit of research, I came across that as well. Thanks again for your help :D
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|