![]() |
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";Dave |
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:
PHP Code:
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. |
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. |
Quote:
Code:
SELECT * FROM db WHERE DATESUB(CURDATE(),INTERVAL 8 HOURS)>=insert_time |
Above code fixed.
|
So if i'm reading this correctly, this should only pull rows that are 8+ hours?
|
That is correct.
|
and insert_time is that the table name? As in my case it would just be "time"?
|
Quote:
|
Wonderful! Thanks so much for the help! If I run into problems, i'm sure i'll be back. Wish me luck!
Thanks again! |
Always glad to help.
|
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"; |
Quote:
Run this: Code:
$sql = "SELECT * FROM credits_temp WHERE DATESUB(CURDATE(),INTERVAL 8 HOURS)>=time"; |
bah, I guess I should of done that from the start anyway :P
This is the error it spit out: Quote:
|
Change HOURS to HOUR
|
Hmm... Still no dice. Exact same error.
|
What is the data type of time? I still haven't gotten a clear answer to that.
|
The command is not DATESUB, its DATE_SUB. I can't believe I missed that, I'm a little rusty on my MySql.
|
I apologize for that, here is a screenshot of the db in phpmyadmin:
![]() |
Ahhh, good timing... just after a bit of research, I came across that as well. Thanks again for your help :D
|
| All times are GMT. The time now is 03:38 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0