-3

I have done this function so that I have as a result the working days without counting the weekend between two dates, in the database I have the date fields as DATATIME, but at the time of executing I get this error:

A non well formed numeric value encountered

The function is as follows:

function diashabiles ($fecha_inicio, $fecha_fin) {

    list ($year, $month, $day) = explode ("/", $fecha_inicio);
    $ini = mktime (0, 0, 0, $month, $day, $year);
    list ($yearf, $mesf, $diaf) = explode ("/", $fecha_fin);
    $fin = mktime (0, 0, 0, $mesf, $diaf, $yearf);
    $newArray = array ();
    $r = 1; $i = 0; $dia2 = 0;

    while ($ini! = $fin) {
    $ini = mktime (0, 0, 0, $month, $day + $r, $year);
    $newArray [$i] = $ini;
    $r++; $i++;
    }


    for ($i = 0; $i <count($newArray); $i++) {
    $day = $newArray [$i];
    $date = getdate($day);
    if ($date["wday"] == 0 or $date["wday"] == 6) {
    $dia2++;
    }
    }

    $rlt = count($newArray) - $dia2;
    return $rlt;

}

Thank You!!

2 Answers2

1

The problem is with this code:

list ($year, $month, $day) = explode ("/", $fecha_inicio);
$ini = mktime (0, 0, 0, $month, $day, $year);
list ($yearf, $mesf, $diaf) = explode ("/", $fecha_fin);
$fin = mktime (0, 0, 0, $mesf, $diaf, $yearf);

You stated that initially, the data stored was in DATE format, which means that what was coming out of the database was simply a year, month and a day. Then, you changed the data type to be DATETIME, which obviously changes what is stored in the database, and obviously changes what is retrieved.

In short, your explode function will leave you with the following:

$year = whatever year is in the database. Can be numeric. $month = whatever month is in the database. Can be numeric. $day = the rest, which will include day, hour, minute, second. Definitely not numeric.

When you use the above information (with the day being a string rather than a number) then mktime() fails.

Peter Abolins
  • 1,520
  • 1
  • 11
  • 18
  • So $day do I have to declare it as whole or as? I do not understand – Alvaro Roman Feb 19 '18 at 09:06
  • The point is that what comes out of the database probably looks like: `2018/01/27 13:05:21`. Now, when you `explode()` this with the `/` separator, you get: `2018`, `01`, and `27 13:05:21`. I am partly guessing, but this is the likely case. So, you need to extract the `27` from the last value... maybe `explode()` it again with the separator as the space character: " "? – Peter Abolins Feb 19 '18 at 09:15
0

Script works well if you replace

while ($ini != $fin)

with this

while ($ini <= $fin).

But i still suggest you to check this link Use DateTime, DateInterval and DatePeriod

Adnan Haider
  • 265
  • 5
  • 16