Hello, I have a problem in my codes. Obviously, I'm beginning to program in PHP and know nothing about its advanced concepts. So I'm asking of your help. I'd like to add 2 months to the end date the user input. I tried this code below:

public function inputEndDate() {
    $value = $this->endDate;
    $html = "";
    $html .= '<label for="enddate">End Date:</label>' . PHP_EOL;
    $html .= '<input type="text" readonly name="enddate" id="enddate" value="'.$value.'">';
    $html .= '<input type="button" id="enddatebutton" onclick="getEndDate()">' . PHP_EOL;
    return $html;
}


public function inputExpiryDate() {
    $value = $this->endDate;
    $date = date('Y/m/d', strtotime("$value +2 month"));
    $html = "";
    $html .= '<label for="expirydate">Expiry Date:</label>';
    $html .= '<input type="text" readonly name="expirydate" id="expirydate" value="'.$date.'">';
    $html .= '<input type="button" id="expirydatebutton" onclick="getExpiryDate()">' . PHP_EOL;
    return $html;
}

But when I run the program, it's not displaying the correct value; however it's displaying the added two months of its start date.

Recommended Answers

All 2 Replies

When I test your code like

<?php

class DateTest {

 public function inputEndDate() {
    $value = date('Y/m/d');
    $html = "";
    $html .= '<label for="enddate">End Date:</label>' . PHP_EOL;
    $html .= '<input type="text" readonly name="enddate" id="enddate" value="'.$value.'">';
    $html .= '<input type="button" id="enddatebutton" onclick="getEndDate()">' . PHP_EOL;
    return $html;
}


public function inputExpiryDate() {
    $value = date('Y/m/d');
    $date = date('Y/m/d', strtotime("$value +6 month"));
    $html = "";
    $html .= '<label for="expirydate">Expiry Date:</label>';
    $html .= '<input type="text" readonly name="expirydate" id="expirydate" value="'.$date.'">';
    $html .= '<input type="button" id="expirydatebutton" onclick="getExpiryDate()">' . PHP_EOL;
    return $html;
}

}

$dateClass = new DateTest();
echo $dateClass->inputEndDate();
echo $dateClass->inputExpiryDate();
?>

It works fine..

You check the value of

 $value = $this->endDate;

The value of endDate is upon the filling of it by the user. It's still empty when you first view the page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.