date increment problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robscriven
Forum Newbie
Posts: 6
Joined: Sun Jan 08, 2006 5:38 pm

date increment problem

Post by robscriven »

Hi,

I've got this code (below) that inserts 5 dates into a mysql database when one is selected. It picks up information from a form and loops through a for loop inserting 5 records incrementing the date by 7 days each time. The first date goes in fine but one the second time through it starts from 1999 eg if i select 2006-01-19 21:48:00 that will be inputted but the following four will be the following:
1999-11-26 09:48:00
1999-12-03 09:48:00
1999-12-10 09:48:00
1999-12-17 09:48:00

Whereas they should be:
2006-01-26 21:48:00
2006-02-02 21:48:00
2006-02-09 21:48:00
2006-02-16 21:48:00

I'm confident the error is in this line

Code: Select all

$date = date("Y-m-d h-i-s", mktime($hour, $min, $second, $month, $day + 7, $year));
but I can't fix it.

I'd be grateful if you had any tips for me.

-Rob

Code: Select all

$date = GetSQLValueString($_POST['lesson_datetime'], "date");
for ($i = 0; $i < 5; $i++)
{

  $insertSQL = sprintf("INSERT INTO booking (bookingid, teacherid, studentid, roomid, lessontypeid, lesson_datetime, cancel) VALUES (%s, %s, %s, %s, %s, %s, %s)",
  
                       GetSQLValueString($_POST['bookingid'], "int"),
                       GetSQLValueString($_POST['teacherid'], "int"),
                       GetSQLValueString($_POST['studentid'], "int"),
                       GetSQLValueString($_POST['roomid'], "int"),
                       GetSQLValueString($_POST['lessontypeid'], "int"),
                       GetSQLValueString($date, "date"),
                       GetSQLValueString($_POST['cancel'], "text"));
					   
	mysql_select_db($database_connection, $connection);
	$Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());

	$year = substr($date, 0, 4);
	$month = substr($date, 5, 2);
	$day = substr($date, 8, 2);
	$hour = substr($date, 11, 2);
	$min = substr($date, 14, 2);
	$second = substr($date, 17, 2);

	$date = date("Y-m-d h-i-s", mktime($hour, $min, $second, $month, $day + 7, $year));
}
}
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try using the mysql function DATE_ADD
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html

Also, be make sure the initial variable $date is the correct format for mysql.

Code: Select all

$nxtDate = 0;
for ($i = 0; $i < 5; $i++)
{

  $insertSQL = sprintf("INSERT INTO booking (bookingid, teacherid, studentid, roomid, lessontypeid, lesson_datetime, cancel) VALUES (%s, %s, %s, %s, %s, %s, %s)",
  
                       GetSQLValueString($_POST['bookingid'], "int"),
                       GetSQLValueString($_POST['teacherid'], "int"),
                       GetSQLValueString($_POST['studentid'], "int"),
                       GetSQLValueString($_POST['roomid'], "int"),
                       GetSQLValueString($_POST['lessontypeid'], "int"),
                       GetSQLValueString(DATE_ADD($date, INTERVAL $nxtDate DAY), "date"),
                       GetSQLValueString($_POST['cancel'], "text"));
                       
    mysql_select_db($database_connection, $connection);
    $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());

$nxtDate += 7;
}
}
Post Reply