BoarderLine Posted September 20, 2010 Share Posted September 20, 2010 I am using the following code to get the third sunday of every month for +6 Months from todays date. I have put together the following code which works perfectly when the date is echoed however when it comes to inserting those dates into mysql db it throws a unable to convert to string error. Im new to this and my head is in a spin as to why those dates can't be inserted if they can echoed ok. I have spent 2 days at it now and searched many forum however they seem to get side tracked from my issue (or do they)??????? I figure I can't go mixing DateTime and date() the way I have? Please help??? [code=php:0] $month=date('F'); $now=date('Y-m-d'); $year=date('Y'); $num='3'; //example only this will be a $_POST value 0-3 $day='Sunday'; //example only this will be a $_POST value Monday-Sunday $start= date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.'')); if($start>$now) { $begin=strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.''); } else { $d = new DateTime( $start ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $year = $d->format( 'Y' ); $begin=strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.''); } $date=date('Y-m-d', $begin); # insert into db first occurance //mysql_select_db($database); //$query_rsFirst = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsFirst = mysql_query($query_rsFirst) or die(mysql_error()); echo $date.'<br/>'; for($i = 0; $i <= 4; $i++) { $d = new DateTime( $date ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $date=date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.'')); # insert into db //mysql_select_db($database); //$query_rsDate = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsDate = mysql_query($query_rsDate) or die(mysql_error()); echo $date.'<br/>'; if($nextmonth=="December") { $d = new DateTime( $date ); $d->modify( 'first day of next year' ); $year = $d->format( 'Y' ); } } [/code] Link to comment https://forums.phpfreaks.com/topic/213948-3rd-sunday-of-every-month-for-6-months-datetime-date-strtotime/ Share on other sites More sharing options...
litebearer Posted September 20, 2010 Share Posted September 20, 2010 1. what data type is you table date field? 2. your query should list the field names with the corresponding variables in the VALUES section Link to comment https://forums.phpfreaks.com/topic/213948-3rd-sunday-of-every-month-for-6-months-datetime-date-strtotime/#findComment-1113473 Share on other sites More sharing options...
BoarderLine Posted September 20, 2010 Author Share Posted September 20, 2010 Thanks litebearer, 1. Date type is 'date' 2. I have adjusted my query [code=php:0] mysql_select_db($database); $query_rsFirst = "INSERT INTO event_date (dateID, eventID, event_dtl_ID, date_start, date_end) VALUES (NULL, '$event', '$d', '$date', '$date')"; $rsFirst = mysql_query($query_rsFirst) or die(mysql_error()); [/code] Still getting error:- Catchable fatal error: Object of class DateTime could not be converted to string in htdocs/Insert/Date_Redirect.php on line 160 Line 160 is : [code=php:0] $query_rsFirst = "INSERT INTO event_date (dateID, eventID, event_dtl_ID, date_start, date_end) VALUES (NULL, '$event', '$d', '$date', '$date')"; [/code] Link to comment https://forums.phpfreaks.com/topic/213948-3rd-sunday-of-every-month-for-6-months-datetime-date-strtotime/#findComment-1113476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.