如何使用php从数据库中提取日期

时间:2019-04-05 17:14:05

标签: php mysql sql datetime

我正在尝试创建每周工作时间表。当我尝试从数据库中选择班次时,我的日期值是从表中选择当前时间,而是日期(Y-m-d)。

这是我的代码:

while ($row = mysqli_fetch_array($res))
{
          $dt = new DateTime();
          $dt->setISODate($year, $week);

          $fetch_mID = $row['ID_EMPLOYEE'];
          $fetch_fn = $row['Firstname'];
          $fetch_en = $row['Lastname'];
          echo "<tr>";
            echo "<td>" . $fetch_fn . " " . $fetch_en . "</td>";

            do {
              $obj = new ReflectionObject($dt);
              $pro = $obj->getProperty('date');
              $date = $pro->getValue($dt);
              echo $date; //Output = 'Y-m-d H:m:s'

              $shift = $conn->query("SELECT ShiftDate, ShiftStart, ShiftEnd FROM shifts WHERE ID_EMPLOYEE = '$fetch_mID' AND Date(ShiftDate) = '$date'");
              $fetch = mysqli_fetch_array($shift);
              $dt->modify('+1 day');
            } while ($week == $dt->format('W'));

我希望$ date输出Y-m-d,但它输出Y-m-d H:m:s。时间就是当前时间。

1 个答案:

答案 0 :(得分:0)

解决方案是将 $ date 转换为日期对象,然后使用新格式 Y-m-d 格式化日期。

dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]

s = 'wanai'
for x, y in zip(dubs, newdubs):
    s = s.replace(x, f'-{y}')

print(s)
# wan-eye

您的新代码似乎类似于:

  $time = strtotime($date);
  $newformat = date('Y-m-d',$time);
  echo $newformat; // output 2019-04-05