在PHP和JavaScript中打印月份名称而不是月份编号

时间:2017-09-07 06:22:27

标签: javascript php jquery html css

我有此代码显示日历滑块以选择日期。它显示日期,如2017-09-17,2017-09-18,2017-09-19。

这是一个显示日期的滑块。

我想显示日期

September-17
September-18
September-19

带月份名称。也不需要年份名称。

<?php 

$page2=$_SERVER["PHP_SELF"];
$page2=explode("/",$page2);
$page2=$page2[count($page2)-1];

$id_sport2=$_GET["id_sport"];

    $m=date("m");
    $day=date("d");
    $year=date("Y");
    $dates=array();
    $dates2=array();
    $tmp=array();
    for($i=1;$i<32; $i++){
        if($i<10){
            $ii="0$i";
        }else{
            $ii=$i;
        }
        $date="$year-$m-$ii";
        //$date = date("Y-m-d", strtotime($date));
        if($i%3!=0){
            array_push($tmp,$date);

            if($i==60){
                if(count($tmp!=0)){
                    array_push($dates2,$tmp);
                }
            }

        }else{
            array_push($tmp,$date);
            array_push($dates2,$tmp);
            $tmp=array();
        }



        array_push($dates,$date);
    }

    ///print_r($dates2);


?>

<div class="row" style="border:1px solid silver; background: #a0a0a0;  padding: 15px 20px; color: white; ">
<div class="MultiCarousel" data-items="1,3,5,6" data-slide="1" id="MultiCarousel2"  data-interval="1000" >
                        <div class="MultiCarousel-inner">



                            <?php 
                    $cpt=0;

                    for($i=0; $i<count($dates2); $i++){

                        $line=$dates2[$i];

                        $today=date("Y-m-d");
                        if(empty($_GET["date"])){
                            $goto=$today;
                        if(in_array($today,$line)){
                            $active="active";


                        }else{
                            $active="";
                        }
                        }else{
                            $dt=$_GET["date"];
                            $goto=$dt;
                            if(in_array($dt,$line)){
                                $active="active";
                            }else{
                                $active="";
                            }
                        }





                            for($x=0; $x<count($line); $x++){

                                $el=$line[$x];
                                if(!empty($_GET["date"])){
                                    $dt=$_GET["date"];
                                    if($el==$dt){
                                        $color="red";
                                    }else{
                                        $color="white";
                                    }
                                }else{
                                if($el==$today){
                                    $color="red";
                                }else{
                                    $color="white";
                                }
                                }



                                $href="$page2?id_sport=$id_sport2&date=$el";
                                if($x==0){
                                    //echo "<div class='col-lg-1 col-xs-1'>&nbsp;</div>";
                                    //echo "<div class='col-lg-10 col-xs-10'>";

                                }

                                    /*echo ' <div class="col-md-4 col-xs-4 col-lg-4 ">
                                <a href="'.$href.'" class="btn btn-default " style="width:90%; font-size:11px; background-color:black; border:1px solid white; color:'.$color.';">'.$el.'</a></div>';
                                */
                                if($x==2){
                                    //echo "</div>";
                                }

                            ?>
                            <div class="item" style="text-align:center">
                                <a href="<?php echo $href; ?>" >
                                    <p class="sportName mydate p-date" style="color:white; font-size: 12px; text-align: center;" ><?php echo $el; ?></p>
                                </a>
                            </div>

                            <?php 

                            }


                    }
                    echo "<input type='hidden' value='$goto' id='cd'>";

                 ?>

                        </div>
                        <button class="btn btn-primary leftLst" style="border-radius: 0px;"><</button>
                        <button class="btn btn-primary rightLst fw" style="border-radius: 0px;">></button>
                    </div></div>

                    <script>
                        $(document).ready(function(){
                            var cd=$("#cd").val();
                            $(".mydate").each(function(){
                                var cd_tmp=$(this).text();
                                if(cd_tmp!=cd){
                                    //alert(cd_tmp+" is different from "+cd);


                                    //$(".fw").click();
                                }else{
                                    $(this).addClass("tag tag-danger");
                                    //break;
                                }
                            })
                        })
                    </script>

3 个答案:

答案 0 :(得分:1)

替换这部分代码:

<p class="sportName mydate p-date" style="color:white; font-size: 12px; text-align: center;" ><?php echo $el; ?></p>

要:

<p class="sportName mydate p-date" style="color:white; font-size: 12px; text-align: center;" >
  <?php 
    //echo $el;

    //convert date to month name
    $date = $el;
    $month_name =  ucfirst(strftime("%B", strtotime($date)));
    $day_number = ucfirst(strftime("%d", strtotime($date)));

    echo $month_name.' - '.$day_number;
  ?>
</p>

答案 1 :(得分:0)

尝试以下代码可以帮助您。在括号内获取您需要“F”的月份的完整名称。我已经删除了IF条件,如果它小于10,你在实际日期后面追加0     

$page2=$_SERVER["PHP_SELF"];
$page2=explode("/",$page2);
$page2=$page2[count($page2)-1];

$id_sport2=$_GET["id_sport"];

$m=date("F");
$day=date("d");

$dates=array();
$dates2=array();
$tmp=array();
for($i=1;$i<32; $i++){

    $date="$m-$d";
    //$date = date("Y-m-d", strtotime($date));
    if($i%3!=0){
        array_push($tmp,$date);

//here you are checking if $i==60, but the maximum limit for $i is 32, what's the point of this check?

        if($i==60){
            if(count($tmp!=0)){
                array_push($dates2,$tmp);
            }
        }

    }else{
        array_push($tmp,$date);
        array_push($dates2,$tmp);
        $tmp=array();
    }



    array_push($dates,$date);
}

///print_r($dates2);

答案 2 :(得分:0)

试试这种方式。将您的标题php代码更改为此代码。

<?php 

$page2=$_SERVER["PHP_SELF"];
$page2=explode("/",$page2);
$page2=$page2[count($page2)-1];

$id_sport2=$_GET["id_sport"];

$m=date("m");
$day=date("d");
$year=date("Y");
$dates=array();
$dates2=array();
$tmp=array();
for($i=1;$i<32; $i++){
    if($i<10){
        $ii="0$i";
    }else{
        $ii=$i;
    }
    $monthObj = DateTime::createFromFormat('!m', $m);
    $monthName = $monthObj->format('F');
    $date="$monthName-$ii";
    if($i%3!=0){
        array_push($tmp,$date);

        if($i==60){
            if(count($tmp!=0)){
                array_push($dates2,$tmp);
            }
        }

    }else{
        array_push($tmp,$date);
        array_push($dates2,$tmp);
        $tmp=array();
    }



    array_push($dates,$date);
}

?>