简体   繁体   中英

using a variable outside while loop

I am trying to use a variable outside of a while loop which is in a foreach loop, but it works in the loop and displays the correct date. But when used outide the loop it displays incorrect date 01/01/1970 .

I would be grateful if someone could point out my error. Many thanks.

  $Destdate = $_POST['box_rtv'];
  $brtvarray = array();
  $brtvarray = $Destdate;
  $ddate = array();

  foreach ($brtvarray as $destbox) {
    $sql = "SELECT destroydate FROM act WHERE item = '".$destbox."'";
    $result = mysqli_query($conn, $sql) or die('Error selecting item: ' . mysqli_error($conn));
    if (mysqli_num_rows($result) > 0) {
      while ($row_code = mysqli_fetch_array($result)) {
      $ddate[] = date('d/m/Y', strtotime($row_code['destroydate']));
      }
    }
  }

    $company = strtoupper(mysqli_real_escape_string($conn, $_POST['rtvcompany']));
    $activity = "Box Retrieval";
    $address = ucwords(mysqli_real_escape_string($conn, $_POST['address']));
    $service = ucwords(mysqli_real_escape_string($conn, $_POST['service']));
    $success = 'SUCCESS';
    $authorised = ucwords(mysqli_real_escape_string($conn,$_SESSION['ls_name_usr']));
    $dept = strtoupper(mysqli_real_escape_string($conn,$_POST['rtvdept']));
    $boxitems = $_POST['box_rtv'];
    $box = implode(",",$boxitems);
    $array = array();
    $array = $boxitems;

    foreach ($array as $boxes) {
        $outString .= "$box" . "  ";

        $sql = "INSERT INTO `act` (service, activity, department, company, address, `user`, `date`, destroydate, item, new) VALUES ('$service', '$activity', '$dept', '$company', '$address', '$authorised', NOW(), '".$ddate."', '$boxes', 1)";
        $result = mysqli_query($conn, $sql) or die ('Error inserting box:' . mysqli_error($conn));

        $sql = "UPDATE boxes SET status = 1 WHERE customer = '$company' AND custref = '$boxes'";
        $result = mysqli_query($conn, $sql) or die ('Error updating box:' . mysqli_error($conn));

        $sql = "UPDATE files SET boxstatus = 1 WHERE customer = '$company' AND boxref = '$boxes'";
        $result = mysqli_query($conn, $sql) or die ('Error updating filebox:' . mysqli_error($conn));
    }

    $json = array($box);
    $result = json_encode($json);
    echo $result;

If your goal is to have array of dates, which is $ddate, then this line

$ddate = date('d/m/Y', strtotime($row_code['destroydate']));

should be:

$ddate[] = date('d/m/Y', strtotime($row_code['destroydate']));

You are not using any standard format for year in date so first you need to make it standard either full year 2018 or short 18

$row_code['destroydate'] = '29/05/018';
$dt_arr = explode("/",$row_code['destroydate']);
$dt_arr[2] = 2000+$dt_arr[2];
$dt = implode("/",$dt_arr);
$date = DateTime::createFromFormat('d/m/Y', $dt);
echo $date->format('Y-m-d');

Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM