提问人:Laquire 提问时间:9/22/2023 最后编辑:ADysonLaquire 更新时间:9/22/2023 访问量:54
如何在 PHP 中编辑日期范围?
How can I edit date ranges in PHP?
问:
我编写了一个系统,该系统根据配额数据将入住日期与酒店合并或分开,并创建一个 excel。一切正常,我只是在日期部分有一个小问题。
让我完全解释一下这个问题:
在分隔状态数据中输入的数据的日期时,它不会分隔某些日期,通常它不分隔的日期是开始日期。我不明白为什么。
我在下面与您分享我的代码块。我也在分享我的数据库数据。我还将分享我的原始输出和我想要获得的输出。
Excel 生成部分。我只分享了进行解析的部分。
/////////////////////// EXCEL ISLEMLERI
$hcode = $veri[0]->hcode;
$rtype = $veri[0]->rtype;
$mailsubject = $veri[0]->mailsubject;
$veriExcel = HALIL::sorgula("SELECT * FROM vakanzfillrooms WHERE hcode = ?", array($hcode));
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'BCH');
$sheet->setCellValue('B1', 'Allotment Request Form');
$sheet->setCellValue('F1', "Sold" . PHP_EOL . "Rooms");
$sheet->getStyle('F1')->getAlignment()->setWrapText(true);
$sheet->setCellValue('G1', "Total" . PHP_EOL . "Allotment");
$sheet->getStyle('G1')->getAlignment()->setWrapText(true);
$sheet->setCellValue('H1', "Please fill" . PHP_EOL . "up" . PHP_EOL . "Extra Allot.");
$sheet->getStyle('H1')->getAlignment()->setWrapText(true);
$columnHeaders2 = ['Hcode', 'Hotel Name', 'Room Type', 'Start Date', 'End Date'];
$col2 = 'A';
foreach ($columnHeaders2 as $header2) {
$sheet->setCellValue($col2 . '2', $header2);
$col2++;
}
usort($veriExcel, function($a, $b) {
$odaTipSonuc = strcmp($a->rtype, $b->rtype);
if ($odaTipSonuc == 0) {
$tarihA = strtotime(str_replace('.', '-', $a->mailsubject));
$tarihB = strtotime(str_replace('.', '-', $b->mailsubject));
if ($tarihA == $tarihB) {
return 0;
}
return ($tarihA < $tarihB) ? -1 : 1;
}
return $odaTipSonuc;
});
$birlestirilmisVeri = [];
$birlestirilmisVeri[] = $veriExcel[0]; // İlk satırı başlangıç olarak ekleyin
for ($i = 1; $i < count($veriExcel); $i++) {
// Eğer veri "vakanzhcode" tablosunda bulunuyorsa birleştirme
$queryDurumExcel = "SELECT * FROM vakanzhcode WHERE hcode = ? AND rtype = ? AND mailsubject = ?";
$paramsDurumExcel = array($veriExcel[$i]->hcode, $veriExcel[$i]->rtype, $veriExcel[$i]->mailsubject);
$resultDurumExcel = HALIL::sorgula($queryDurumExcel, $paramsDurumExcel);
if (!empty($resultDurumExcel))
{
// "vakanzhcode" tablosunda veri bulunuyorsa
$durumBirlestirmeExcelSorgusu = 'SELECT durum FROM vakanzhcode WHERE hcode = ? AND rtype = ? AND mailsubject = ?';
$durumExcel = HALIL::sorgula($durumBirlestirmeExcelSorgusu, array($veriExcel[$i]->hcode, $veriExcel[$i]->rtype, $veriExcel[$i]->mailsubject));
if ($durumExcel != 4)
{
$birlestirilmisVeri[] = $veriExcel[$i];
continue;
}
}
else
{
// "vakanzhcode" tablosunda veri bulunmuyorsa diğer koşulları kontrol et
$onceki = strtotime(str_replace('.', '-', $veriExcel[$i - 1]->mailsubject));
$suanki = strtotime(str_replace('.', '-', $veriExcel[$i]->mailsubject));
$birGun = 24 * 60 * 60; // 1 günün saniye cinsinden değeri
if ($suanki - $onceki === $birGun && $veriExcel[$i - 1]->hcode === $veriExcel[$i]->hcode) {
if ($veriExcel[$i - 1]->total_kontenjan === $veriExcel[$i]->total_kontenjan &&
$veriExcel[$i - 1]->dolu_kontenjan === $veriExcel[$i]->dolu_kontenjan) {
$birlestirilmisVeri[count($birlestirilmisVeri) - 1]->mailsubject .= ' - ' . $veriExcel[$i]->mailsubject;
} else {
$birlestirilmisVeri[] = $veriExcel[$i];
}
} else {
$birlestirilmisVeri[] = $veriExcel[$i];
}
}
}
$row = 3;
$rowHeader = 1;
foreach ($birlestirilmisVeri as $veriex) {
$otelKodu = $veriex->hcode;
$otelAdi = $veriex->hname;
$odaTipi = $veriex->rtype;
$dolulukTarihAraliklari = $veriex->mailsubject;
$totalKontenjan = $veriex->total_kontenjan;
$doluKontenjan = $veriex->dolu_kontenjan;
$tarihAraligi = explode(' - ', $veriex->mailsubject);
$baslangicTarihi = $tarihAraligi[0];
$bitisTarihi = end($tarihAraligi);
$sheet->setCellValue('A' . $row, $otelKodu);
$sheet->setCellValue('B' . $row, $otelAdi);
$sheet->setCellValue('C' . $row, $odaTipi);
$sheet->setCellValue('D' . $row, $baslangicTarihi); // Başlangıç tarihini D sütununa yazın
$sheet->setCellValue('E' . $row, $bitisTarihi); // Bitiş tarihini E sütununa yazın
$sheet->setCellValue('F' . $row, $totalKontenjan);
$sheet->setCellValue('G' . $row, $doluKontenjan);
SQL 数据库中的数据示例 (vakanzhcode):
INSERT INTO `vakanzhcode` (`id`, `hcode`, `durum`, `rtype`, `mailsubject`) VALUES
(905, 'ANDEI', '3', 'ESS EINZELZIMME', '05.11.2023'),
(904, 'ANDEI', '3', 'ESS EINZELZIMME', '04.11.2023'),
(903, 'ANDEI', '3', 'ESS EINZELZIMME', '16.03.2024'),
(902, 'ANDEI', '3', 'ESS EINZELZIMME', '15.03.2024'),
(901, 'ANDEI', '3', 'ESS EINZELZIMME', '10.03.2024');
SQL 数据库中的数据示例 (vakanzfillrooms):
INSERT INTO `vakanzfillrooms` (`id`, `hcode`, `hname`, `rtype`, `mailsubject`, `mail`, `total_kontenjan`, `dolu_kontenjan`) VALUES
(429, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '04.11.2023', '[email protected]', 4, 4),
(430, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '05.11.2023', '[email protected]', 4, 4),
(434, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '10.03.2024', '[email protected]', 2, 2),
(439, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '15.03.2024', '[email protected]', 2, 2),
(440, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '16.03.2024', '[email protected]', 2, 2);
原始输出:
我想要的输出:
显然,我已经尝试了许多不同的代码,但没有一个给我任何结果,可能我正在其他地方寻找问题。我想我需要第三只眼睛。
答:
0赞
Kumara
9/22/2023
#1
尝试使用 DateTime 对象进行日期比较 -->
if ($odaTipSonuc == 0) {
$tarihA = new DateTime(str_replace('.', '-', $a->mailsubject));
$tarihB = new DateTime(str_replace('.', '-', $b->mailsubject));
if ($tarihA == $tarihB) {
return 0;
}
return ($tarihA < $tarihB) ? -1 : 1;
}
您的数据 进入 vakanzfillrooms 只有 5 条记录
INSERT INTO `vakanzfillrooms` (`id`, `hcode`, `hname`, `rtype`, `mailsubject`, `mail`, `total_kontenjan`, `dolu_kontenjan`) VALUES
(429, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '04.11.2023', '[email protected]', 4, 4),
(430, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '05.11.2023', '[email protected]', 4, 4),
(434, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '10.03.2024', '[email protected]', 2, 2),
(439, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '15.03.2024', '[email protected]', 2, 2),
(440, 'ANDEI', 'Delphin Imperial', 'ESS EINZELZIMME', '16.03.2024', '[email protected]', 2, 2);
因此,输出最多只有 5 条记录,因为它会循环访问来自 Vakanzfillrooms 的记录,即使您这样做
SELECT * FROM vakanzfillrooms
评论
0赞
Laquire
9/22/2023
结果是一样的
0赞
Kumara
9/22/2023
该代码最多只能生成 5 条与其余数据无关的记录,因为 $veriExcel = HALIL::sorgula(“SELECT * FROM vakanzfillrooms WHERE hcode = ?”, array($hcode));将只给出 5 条记录。而 for ($i = 1; $i < count($veriExcel); $i++) 循环最多只有 5 次。
0赞
Laquire
9/22/2023
“ $queryVeriExcel = ”SELECT * FROM vakanzfillrooms WHERE hcode = ?“;$paramsVeriExcel = 数组($hcode);$veriExcel = HALIL::sorgula($queryVeriExcel, $paramsVeriExcel);“ 我像这样更改了编码,没有任何区别
0赞
Kumara
9/22/2023
您的表中只有 5 条记录,这是 Vakanzfillrooms 中的记录数。因此,您在输出中获得的最大记录数为 5 ,甚至您的 sql 查询也使用 SELECT * FROM vakanzfillrooms 。您的数据输入只有 5 条记录 INSERT INTO (, , , , ) 值 (905, 'ANDEI', '3', 'ESS EINZELZIMME', '05.11.2023'), (904, 'ANDEI', '3', 'ESS EINZELZIMME', '04.11.2023'), (903, 'ANDEI', '3', 'ESS EINZELZIMME', '16.03.2024'), (902, 'ANDEI', '3', 'ESS EINZELZIMME', '15.03.2024'), (901, 'ANDEI', '3', 'ESS EINZELZIMME', '10.03.2024');vakanzhcode
id
hcode
durum
rtype
mailsubject
0赞
Laquire
9/22/2023
不,我的表中没有 5 条记录,我只是给出了那么多记录的信息来解释问题。通常,我的记录计数显示第 0 - 24 行(总共 2092 行,查询需要 0.0088 秒。[邮件主题: [BLOB - 10 B]... - [BLOB - 10 B]...]
上一个:拉取值行/值范围的 SQL 数据
评论