简体   繁体   English

在while循环外使用变量

[英]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. 我正在尝试在foreach循环中的while循环之外使用变量,但是它在循环中起作用并显示正确的日期。 But when used outide the loop it displays incorrect date 01/01/1970 . 但是当在循环外使用时,它会显示错误的日期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,则此行

$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 您没有使用任何标准格式, yeardate所以首先你需要使它标准无论是全年2018或短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 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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