1
votes

I am trying to use PHP to change a picture in a different DIV based on the number of days in a month (28, 29, 30, or 31), when I click on a date in a Jquery Datepicker.

Example... Today is the 15th of February 2017, so if I click the date 3 February 2017 on the inline datepicker, the DIV would show an image based on a 28 day month, if I then clicked on the date 22 December 2016, the DIV would display an image based on a 31 day month.

2
Show your code before we point out what you have done wrong.AkiEru
PHP isn't what you are looking for. This would be done in javascript/jquery. PHP is server side; essentially it generates the HTML for you. Anything that happens before the page is presented in the browser could be done in PHP. Stuff you want to change once the page is displayed should probably be done in js.BizzyBob
Show us what you had tried yet ...Pranav MS

2 Answers

0
votes

This is how you can get number of days in a particular month. See the working fiddle

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>
0
votes

In PHP, You can get the number of days in this month like this:

<?php

$numDays = (int) date('j', strtotime('last day of this month'));

or if you're using PHP7,

<?php

$numDays = (int) (new DateTime("last day of this month"))->format('j')

If you want to specify the month and year, you may use month and year in the relative date string. e.g. "last day of February 2004" instead of "last day of this month".

See Relative Date and date() for details.