0

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;
3
  • share sample value for $row_code['destroydate'] ? Commented Jun 16, 2018 at 11:01
  • @C2486 should be: 29/05/018 Thanks Commented Jun 16, 2018 at 11:07
  • Are you sure year is 018 and not 2018 or 18 ? Commented Jun 16, 2018 at 12:49

2 Answers 2

3

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']));
Sign up to request clarification or add additional context in comments.

6 Comments

That works but not when I use variable in mysql query. it enters date as 0000-00-00. I am trying to do a lookup on a table and then use that var $ddate in a foreach loop for a mysql insert. I shall update my code to reflect this.
If $row['destroydate'] = "29/05/2018", then $ddate[]=$row['destroydate']
If in your MySQL table your field format is datetime then you should convert your date to proper format before using it in INSERT query, which is Y-m-d H:i:s. You can read more about in this question stackoverflow.com/questions/2501915/…
It is a date field. Thanks
you have $ddate as an array, but in your second foreach loop, you're using it as a scalar in your INSERT query. Echo the resulting SQL query string and see what it looks like. I think you'll find it's not what you expected.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.