Web Hosting Talk







View Full Version : MySQL with Cron job(date detection)


stuffradio
06-07-2007, 01:52 AM
I want to run a cron job once a day, check if an entry in a database has been there for a week or not. If it has, do something. If not, leave it. Can anyone help me with this?

Ks Jeppe
06-07-2007, 05:16 AM
Just make a php/asp/(Whatever your favourite scripting language is) script and run that via. the cron job? :) It's not really that hard :)

stuffradio
06-07-2007, 01:04 PM
I don't know how to use the date function in sql to detect if it's been 7 days after the creation part. That's the hard part...

foobic
06-08-2007, 01:29 AM
Here is an example that uses date functions. The following query selects all rows with a date_col value from within the last 30 days:

mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;

This and lots of other useful information here (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html)

stuffradio
06-08-2007, 11:41 PM
To me this seems like the thing I can use:


$nextweek = mktime(0, 0, 0, date("m") , date("d")+7, date("Y"));


would it work if I went:

$nextweek = mktime(0, 0, 0, date("m") , date("d")+7, date("Y"));

if ($user['created'] == $nextweek) {
// Execute mysql update here
}

Seems like that should work to me, but want your opinions

Ks Jeppe
06-09-2007, 09:00 AM
Well, to help you, we'd need to know which format your database stores the times in? It's is UNIX timestamps, or mysql TIME/DATE fields? :) If you'd like, you can pm me with an msn/irc address and I can try to help you via. that instead of here, since it seems to be a bit harder via. the forums? (no offence meant, but you don't seem to have a lot of experience with things like this, which is why i suggest IM)