Jump to content

3rd Sunday of every month for 6 Months (DateTime, date(), strtotime())????


BoarderLine

Recommended Posts

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]

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]

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.