我在wordpress网站上有一个重力表,这个月的每个星期二都会有。我希望修改目前在functions.php中的代码,使其仅显示第一和第三个星期二。

    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;

最佳答案

有很多不同的方法可以做到这一点,下面的解决方案不会是最优雅的。不过,它将足够,并可能满足您的需要。

<?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'));
}

这会给你一个星期二的所有日子。
只需使用[0]和[2]索引进行验证,以仅选择本月的第一个和第三个星期二。
例子:
$today = date('d');
if($today === $tuesdays[0] || $today === $tuesdays[2]){
  //Do your stuff
}

基于您的代码:
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;
}

关于php - 如何仅将本月的第一个和第三个星期二拉入PHP的表单字段中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41448944/

10-11 05:34