-1

I want to calculate the days by write two the dates in html form we I did the code bellow I have this problem.

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Pos_Date::setDate(), 1 passed in C:\xampp\htdocs\dateTimeZone\index.php on line 40 and exactly 3 expected in C:\xampp\htdocs\dateTimeZone\Date.php:116 Stack trace: #0 C:\xampp\htdocs\dateTimeZone\index.php(40): Pos_Date->setDate('2011, 5, 5') #1 {main} thrown in C:\xampp\htdocs\dateTimeZone\Date.php on line 116

Note if I put for example 2019, 1 ,1 instead $date1 and 2020, 1 1 instead $date2 after in setDate, it works and echo 365 days left. in my class on line 116 I have this code public function setDate($year, $month, $day)

you can see the whole class and my code by this link. https://drive.google.com/drive/folders/1D406OsQyUEI4MKjSjs3XRWl4aiDyIBAt?usp=sharing

<form action="index.php" method="post">
    <input type="text" name="date1">
    <input type="text" name="date2">
    <input type="submit" name="submit">
</form>

here is php code

$date1 = $_POST['date1'];

$date2 = $_POST['date2'];

if(isset($_POST['date1']) && isset($_POST['date2']) && isset($_POST['submit'])){

try { 
    $now = new Pos_Date();
    $newYear = new Pos_Date();
    $now->setDate(2019, 1, 1);
    $newYear->setDate($date2);
    $diff = $now->dateDiff2($newYear);
    $unit = abs($diff) > 1 ? 'days' : 'day';

    if ($diff > 0) {
        echo "$diff $unit left";
    } 

} catch (Exception $e) {
    echo $e;
}

}

core114
  • 5,155
  • 16
  • 92
  • 189

2 Answers2

0

the setDate() method requires the date to be passed in the following format

public DateTime DateTime::setDate ( int $year , int $month , int $day )

So you have to format your date prior to sending it to the method, which can be done using

$date2->format('DD') // for day
$date2->format('Y') // for year
$date2->format('M') // for month

http://php.net/manual/en/datetime.setdate.php

Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
  • public function setDate( $year, $month, $day) { if (! is_numeric ( $year ) || ! is_numeric ( $month ) || ! is_numeric ( $day )) { throw new Exception ( 'setDate() expects three numbers separated by commas in the order: year, month, day.' ); } if (! checkdate ( $month, $day, $year )) { throw new Exception ( 'Non-existent date.' ); } parent::setDate ( $year, $month, $day ); $this->_year = ( int ) $year; $this->_month = ( int ) $month; $this->_day = ( int ) $day; I have it in my class and I think its same thing – youssef maia Jan 10 '19 at 04:04
  • this application understand that is a date because I cloud everything in my class and when I write a time instead a variable its work but I want to write time in a text input without changing the class. when I do it with POST method it gave like when I wrote time between two single quot like setDate('2010,1,1') I want to read it like that setDate(2010,1,1) without single quot I wonder if other type to POST it to setDate. in my app now if I put two dates without posting to html it's work right thanks for your aswer – youssef maia Jan 10 '19 at 04:16
0

I have make a function that returns difference of two dates in Years,months,days,hours,minutes and seconds. If dates difference in years than it gives only years respectively.

$diff = abs(strtotime($firstime) - strtotime($secondTime));
function timediff($diff) {
    $years = floor($diff / (365 * 60 * 60 * 24));
    if (empty($years)):
        $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
        if (empty($months)):
            $days = floor(($diff - $years * 365 * 60 * 60 * 24 -
                    $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
            if (empty($days)):
                $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
                if (empty($hours)):
                    $minutes = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
                    if (empty($hours)):
                        $minutes = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
                        if (empty($minutes)):
                            $seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minutes * 60));
                        endif;
                    endif;
                endif;
            endif;
        endif;
    endif;

I hope this will works better for you. Thanks, sandeep

Sandeep K.
  • 759
  • 6
  • 18