我试图使用PHP根据一个月中(28、29、30或31天)的天数在不同的DIV中更改图片,当我单击Jquery中的日期时。
例如..。今天是2017年2月15日,所以如果我在内联数据采集器上单击2017年2月3日的日期,DIV将显示一个基于28天月的图像,如果我在2016年12月22日单击,DIV将显示一个基于31个月的图像。
发布于 2017-02-15 07:04:12
这就是在一个月中你可以得到多少天的方法。见工作小提琴
var numDays = new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate();
Date.prototype.monthDays = function() {
var d = new Date(this.getFullYear(), this.getMonth() + 1, 0);
return d.getDate();
}
$(function() {
var datepicker = $("#datepicker").datepicker({
onSelect: function(date) {
var numberOfDays = new Date(date).monthDays();
var src = '';
switch (numberOfDays) {
case 28:
src = 'http://4vector.com/i/free-vector-28-clip-art_115408_28_clip_art_medium.png';
break;
case 30:
src = 'http://www.clipartkid.com/images/135/30-lessons-learned-in-30-years-a-good-run-Ergij9-clipart.jpg';
break;
case 31:
src = 'https://img.clipartfest.com/fb17991d9c81ea61b1a2950086928603_-vector-number-31-clip-art-number-31-clipart_300-249.png';
break;
case 29:
src = 'http://www.drodd.com/images16/29-14.jpg';
break;
}
$('#img').attr('src', src);
}
});
});#img {
height: 80px;
width: 80px;
border: none;
}<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date:
<input type="text" id="datepicker">
</p>
<p>
<img src="" id="img">
</p>
发布于 2017-02-15 07:24:54
在PHP中,您可以得到这个月的天数,如下所示:
<?php
$numDays = (int) date('j', strtotime('last day of this month'));或者如果你在使用PHP7,
<?php
$numDays = (int) (new DateTime("last day of this month"))->format('j')如果要指定月份和年份,可以在相对日期字符串中使用月份和年份。"last day of February 2004"而不是"last day of this month"。
详细信息请参见相对日期和日期()。
https://stackoverflow.com/questions/42241896
复制相似问题