0
votes

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;
}

}

2

2 Answers

0
votes

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

0
votes

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