Link to home
Create AccountLog in
Avatar of revo1059
revo1059Flag for United States of America

asked on

Script that will insert the date of every friday into a mysql database

i have a table called lt_weekend.  I want to be able to insert the date of all the fridays in the year 2006. I am using php 4 and mysql 5.0.18. Please help
Avatar of campbelc
campbelc

I'm a major newbie with php but pretty good with MySQL. Do you have console access to MySQL or can you give me a sample php insert statement that I can replicate for you.

Example:
insert into it_weekend values ('01-06-2006');
insert into it_weekend values ('01-13-2006');

etc...

Also what other columns are there in your it_weekend table?
Avatar of Arty K
The algorithm is:
1) Find 1st friday of 2006, it's a $start_day
2) While year of $curr_day is 2006:
  insert $start_day into database
  add 7 days to $start_day


// Begin from 2006-01-01
$start_day=mktime(0, 0, 0, 1, 1, 2006);
// Find 1st friday
for($i=1; $i<=7; $i++) {
  if (gmdate("N", $start_day)=="5") {
    break;
  }
  $start_day+=24*60*60; // next day
}

// Now start_day is a first friday in unix timestamp format (integer)
while (gmdate("Y", $start_day)!="2007") {
   // for appropriate time convertion look: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html
   // I suppose you have a TIMESTAMP type for your_date_field
   $query = "INSERT INTO lt_weekend (your_date_field) VALUES ($start_day)";
   $link.mysql_query($query);
  $start_day+=7*24*60*60;
}
SOLUTION
Avatar of todd_farmer
todd_farmer
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.

Note : The delete statement in your case should be
DELETE FROM it_weekend where YEAR(your_date_column) > '2006';
Avatar of revo1059

ASKER

I like the idea of a stored procedure, because i will need to do this for all the years to come.  When I pasted the SP into my mysql command line, it gave me an error


ERROR 1064 (42000): 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 'INT
SET v = 0
WHILE v < 54 DO
INSERT INTO  lt_weekend VALUES(DATE_ADD('2006-01-0' at line 3


any ideas.  

also, the name of the table is lt_weekend... lt stands for list table.

thanks
Date string is incorrect: '2006-01-0', there is no 0 day in a month
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
CREATE PROCEDURE p01 ()
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 54 DO
INSERT INTO  lt_weeks VALUES(DATE_ADD('2006-01-06', INTERVAL v WEEK));
SET v = v + 1;
END WHILE;
DELETE FROM lt_weeks WHERE YEAR(weekend) > '2006';
END; //

this is what i typed into the mysql command line.  it cut off the rest of the line.  I put in the full date 2006-01-06.


The table is called lt_weeks and the dave field is weekend
                          < L NOT i  >
DELIMITER //
CREATE PROCEDURE p01 ()
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 54 DO
INSERT INTO  lt_weeks VALUES(DATE_ADD('2006-01-06', INTERVAL v WEEK));
SET v = v + 1;
END WHILE;
DELETE FROM lt_weeks WHERE YEAR(weekend) > '2006';
END; //

THIS WAS THE FINAL CODE AND IT WORKED GREAT.  THANK YOU
what error are you getting now?