Jump to content

[SOLVED] What should I use to set up modified "Calendar" form?


Jackanape

Recommended Posts

I have events that take place every Monday, Wednesday, Saturday, and Sunday, and I want to generate my copy--which is the same for most events--and my printed schedule from a php form.

 

My plan is to first generate a form that lists pulldown boxes for each of those days in the next month--I could specify the month, but I don't think that fits my bill, and then utilize the pulldowns to set the events.

 

However, I'm not sure where to begin to return just the days I need...should I use getdate() as my basis for this?  From what I'm reading, it seems right, but I'm not sure...any advice on that is helpful.

I've tried a few pokes at this, but I still come up empty...I hate to keep bumping this, but I'm at my wit's end...

 

All I need to know, really, is what php function I should use for above:  grabbing every Monday from next month, for example...

 

I considered using a calendar function and engineering it to leave out the other days...that may ultimately be what I end up doing, but it seems the wrong way to go about it...

Here is something I would try. Write up a function that will return an array of timestamps for the criteria you need. You will have to use a combination of date/time manipulation to pull it off, but it could be very useful if you go about it the right way. Here is a simple function that will let you pass in the year, month and day of the week you need as parameters and will return the timestamp for all the matching dates:

<?php
function getMyDays($weekday, $month = '', $year = '') {
  $month = empty($month) ? date('m') : $month;
  $year  = empty($year) ? date('Y') : $year;
  $ts   = mktime(0,0,0,$month,1,$year);
  $days  = date('t', $ts);

  $matches = array('raw' => array(), 'formatted' => array());
  for ($i = 1; $i <= $days; $i++) {
    $ts = mktime(0,0,0,$month,$i,$year);
    if ($weekday == date('w', $ts)) {
		$matches['raw'][] = $ts;
		$matches['formatted'][] = date('Y-m-d', $ts);
	}
  }

  return $matches;
}

$myDays = getMyDays(5, '05');
print_r($myDays);
?>

 

Hope this helps!

This was a huge help, and sent me in the right direction!  Thanks!  After a few modifications to the function, I was able to produce an entire listing of "Next Month's" Sundays, Mondays, Wednesdays, and Saturdays:

 

<?php
function getMyDays($month = '', $year = '') {
  $month = empty($month) ? date('m', strtotime('+1 month')) : $month;
  $year  = empty($year) ? date('Y') : $year;
  $ts   = mktime(14,30,0,$month,1,$year);
  $days  = date('t', $ts); 
  
  $matches = array('raw' => array(), 'formatted' => array());
  for ($i = 1; $i <= $days; $i++) 
  		{
    $ts = mktime(14,30,0,$month,$i,$year);
    if ( date('w', $ts) == 0 || date('w', $ts) == 1 || date('w', $ts) == 3 || date('w', $ts) == 6 ) 
	{
		$matches['raw'][] = $ts;
		$matches['formatted'][] = date('g:i A l, F j, Y', $ts);
	}
  }

  return $matches;
} 

function table( $array ) {
    $array = array_values( $array );
   
    $keys = array_keys( $array[0] );
   
    echo '<table border="1"><tr>';
    foreach( $keys as $key ) {
        echo '<td>'.$key.'</td>';
    }
    echo '</tr>';
   
    foreach( $array as $row ) {
        echo '<tr>';
        foreach( $row as $value ) {
            echo '<td>'.$value.'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
}

$myDays = getMyDays();
table($myDays);  
?>

 

I've added a table function that someone sent me along the line (I'm not sure who, so I can't offer credit), in an effort to print out my results in a "form" I can use to pass data during the next step...The table function leaves alot to be desired, as I can't seem to get my 'raw' and 'formatted' data to appear in the same table cell...

 

The whole thing works, however, so I shouldn't complain--I will try to grok how to get the values to appear in the same cell in the next day or so...if anyone has any good ideas, you just know I'm all ears!

 

 

OK--

 

$matches is a multidimensional array, but when the function returns it, I want to pull out the $matches['raw'][$i] and $matches['formatted'][$i] via a for loop, but it's not letting me output the array like that...

 

What am I missing?

 

aha!

 

Here's the solution:

 

$keys = array_keys( $myDays['raw'] );
$keys = count($keys); 

echo '<table border="1"><tr>';
for($s = 0; $s < $keys; $s++) 
{
echo '<td>';
print $myDays['raw'][$s] . '<br />';
print $myDays['formatted'][$s] . '<br />';
echo '</td>';
}
echo '</tr></table>';

 

Now, I can format my output any way I need to for the form fields...sorry about all these posts, but sometimes I just ain't so smart, y'know??

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.