Keshia Angelina Keshia Angelina - 1 year ago 83
MySQL Question

HTML5 & PHP : Get & Convert MONTH Input Type Value into STRING and Minus One Month From Input Value

Stackoverflow community. Recently i just have unique experience. Been Google, trial and error for hours but still not find a solution for this. Input and desired output is actually pretty simple but tricky, i just dont know how to code it properly.

Well, i will define the requirement.

Explanation and Requirement :

  1. I have an input field. This input field type is MONTH (not DATE). So basically this input field should only produce year and month, different from DATE input type that will produce complete date.

  2. I want to echo value in this MONTH input field after converted to STRING before (example : "2016-7" in STRING format) and also produce something like "Jul 2016".

This MONTH format is similar like DATE format, but script that usually work with DATE format, not working with MONTH format. Thats make me wondering, im curious why.

Experiment :

For basic input type MONTH i use this script.

<input type="month" name="monthTransaction" value="<?php echo date("Y-m") ?>"/>

Then i tried something like this to echo it.

$month = mysql_real_escape_string($_POST['monthTransaction']);
$newmonth = date('Y-m', strtotime(str_replace('-', '/', $tr)));
echo $month; // Probably STRING format (i need further explanation)
echo $newmonth; // Probably DATE format (i need further explanation)

This is not working after all. But i tried this with input field with DATE type, its working. The echo fine with DATE type format.

From this code i learn that actually strtotime isn't convert DATE to string. I not know it well but it works well (can echo) with DATE format but not with MONTH format.

If you can tell me how to convert MONTH input type to STRING, it will be so great, because i can learn from that.

Desired Output :

  1. It will print in basic format like "2016-07"

  2. It will print in advanced format like "July 2016"


Second Question (Minus One Month From Input Value) :

Well it's actually another question that related with this.

Second Question Explanation and Requirement :

  1. I actually want to get input value in MONTH input field minus one (-1) month from input value.

Second Question Desired Output :

  1. So if you enter "2016-07" for example, it will give "2016-06" and so on.

DATE format is ordinary, but MONTH format seems challange me. Anybody know how to fix this ? As i said simple but tricky. Btw, thank you in advance.

Answer Source

You can use DateTime class

$month = mysql_real_escape_string($_POST['monthTransaction']);
$temp = new DateTime($month.'-01');
$newmonth = $temp->format('Y-m');
$newMonthName = $temp->format('F Y');
$newMonthMinusOne = $temp->sub(new DateInterval('P1D'))->format('Y-m');
$newMonthMinusOneName = $temp->sub(new DateInterval('P1D'))->format('F Y');

With input '2016-07' the values (all strings) will be:

  • 2016-07
  • July 2016
  • 2016-06
  • June 2016
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download