i want to query less than 1 month from below data
$mysqli = new mysqli($server, $username, $password, $database);
// cek koneksi
if ($mysqli->connect_error) {
die('Koneksi Database Gagal : '.$mysqli->connect_error);
}
$query = mysqli_query($mysqli, "SELECT tgl_exp FROM i_test ")
or die('Ada kesalahan pada query tampil Data Barang: '.mysqli_error($mysqli));
while ($data = mysqli_fetch_assoc($query))
{
echo "$data[tgl_exp]<br>";
}
$newdate = date('Y-m-d',strtotime('-1 month',strtotime($data)));
echo $newdate;
but why the result is wrong like picture below?
please give me the clue, thank you for the help
CodePudding user response:
You need to call strtotime()
inside the loop, when $data
contains a valid row. After the loop is done, $data
is empty.
And the argument has to be $data['tgl_exp']
. $data
is the whole row, which is an array.
$mysqli = new mysqli($server, $username, $password, $database);
// cek koneksi
if ($mysqli->connect_error) {
die('Koneksi Database Gagal : '.$mysqli->connect_error);
}
$query = mysqli_query($mysqli, "SELECT tgl_exp FROM i_test ")
or die('Ada kesalahan pada query tampil Data Barang: '.mysqli_error($mysqli));
while ($data = mysqli_fetch_assoc($query))
{
echo "$data[tgl_exp]<br>";
$newdate = date('Y-m-d',strtotime('-1 month',strtotime($data['tgl_exp'])));
echo $newdate . '<br>';
}
$newdate = date('Y-m-d',strtotime('-1 month',strtotime($data)));
echo $newdate;