gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:46:39 24 4
gpt4 key购买 nike

我正在尝试制定每周工作计划。当我尝试从数据库中选择类次时,我的日期值选择当前时间,但从表中选择日期(年月日)。

这是我的代码:

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。并且时间是当前时间。

最佳答案

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

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

你的新代码似乎是这样的:

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);
//new code
$newformat = date('Y-m-d',strtotime($date));
echo $newformat; // output 2019-04-05
// end new code
$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'));

关于php - 如何使用php从数据库中提取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com