Question: How do i pull in only the first and third Tuesdays of current month into a form field in PHP?

Question

How do i pull in only the first and third Tuesdays of current month into a form field in PHP?

Answers 1
Added at 2017-01-03 18:01
Tags
Question

I have a Gravity Form currently on a Wordpress website that pulls in every Tuesday of the current month. I am looking to modify this code that I currently have in my functions.php to show only the first and third Tuesdays.

    foreach ( $form['fields'] as &$field ) {

        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
            continue;
        }


        $choices = array();
        $session = "Healthy Transformation ";
        // If today is Tuesday, we start with today.  Otherwise we start with the next Tuesday:
        $tuesday = new DateTime();
        $today = date( "D", $tuesday);
        if ( $today != 'Tue' ) {
            $tuesday->modify('next tuesday');
        }
        for ( $i=0; $i<4; $i++ ) {
            $new_sess = $session.$tuesday->format('F j, Y');
            $choices[] = array( 'text' => $new_sess, 'value' => $new_sess );
            $tuesday->modify('+7 days');
        }
        $field->placeholder = 'Select an Info Session Date';
        $field->choices = $choices;     
    }

    return $form;
Answers to

How do i pull in only the first and third Tuesdays of current month into a form field in PHP?

nr: #1 dodano: 2017-01-03 18:01

There are a lot of different ways to do this and the solution below won't be the most elegant. However, it will suffice and probably meet your needs.

<?php
//Get the first day of the month
$day = 1;
$month = date('m');
$year = date('Y');

$date = new DateTime($year.'-'.$month.'-'.$day);
//Create the output array
$tuesdays = [];

//Loop through all the days in the month
while($date->format('m') == $month){
    //If today is a tuesday; safe the day number
    if($date->format('D') == 'Tue') $tuesdays[] = $date->format('d');
    //Add one day to the date object
    $date->add(new DateInterval('P1D'));
}

This will give you all the days that is is a tuesday. Just verify with the [0] and [2] index to select only the first and third Tuesday of the month.

Example:

$today = date('d');
if($today === $tuesdays[0] || $today === $tuesdays[2]){
  //Do your stuff
}

Based on your code:

foreach ( $form['fields'] as &$field ) {
    if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
        continue;
    }
    $choices = array();
    $session = "Healthy Transformation ";
    // If today is Tuesday, we start with today.  Otherwise we start with the next Tuesday:
    $tuesdays = getTuesdays();
    //The first tuesday:
    $tuesday = new DateTime(date('Y').'-'.date('m').'-'.$tuesdays[0];
    $new_sess = $session.$tuesday->format('F j, Y');
    $choices[] = array( 'text' => $new_sess, 'value' => $new_sess );
    //The second tuesday:
    $tuesday = new DateTime(date('Y').'-'.date('m').'-'.$tuesdays[2];
    $new_sess = $session.$tuesday->format('F j, Y');
    $choices[] = array( 'text' => $new_sess, 'value' => $new_sess );
    $field->placeholder = 'Select an Info Session Date';
    $field->choices = $choices;     
}

return $form;
}

function getTuesdays(){
   //Get the first day of the month
   $day = 1;
   $month = date('m');
   $year = date('Y');

   $date = new DateTime($year.'-'.$month.'-'.$day);
   //Create the output array
   $tuesdays = [];

   //Loop through all the days in the month
   while($date->format('m') == $month){
     //If today is a tuesday; safe the day number
     if($date->format('D') == 'Tue') $tuesdays[] = $date->format('d');
     //Add one day to the date object
     $date->add(new DateInterval('P1D'));
   }
   return $tuesdays;
}
Source Show
◀ Wstecz