revo1059
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
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;
}
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
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';
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
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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
CREATE PROCEDURE p01 ()
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 54 DO
INSERT INTO lt_weeks VALUES(DATE_ADD('2006-01-0 6', 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.
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 54 DO
INSERT INTO lt_weeks VALUES(DATE_ADD('2006-01-0
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.
ASKER
The table is called lt_weeks and the dave field is weekend
< L NOT i >
< L NOT i >
ASKER
DELIMITER //
CREATE PROCEDURE p01 ()
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 54 DO
INSERT INTO lt_weeks VALUES(DATE_ADD('2006-01-0 6', 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
CREATE PROCEDURE p01 ()
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 54 DO
INSERT INTO lt_weeks VALUES(DATE_ADD('2006-01-0
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?
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?