gpt4 book ai didi

php - 确定星期一和星期五代码停止工作

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:48 25 4
gpt4 key购买 nike

我在数据库中有一些条目,我想按它们发生的那一周进行分组。我想要做的是确定条目发生在一年中的哪一周,然后显示该周以及该周的星期一和星期五。本页http://princeave.com/bo/test.php显示我得到的输出。请注意,星期一和星期五对于 2017 年发生的事件是正确的,但对于 2018 年发生的事件则不正确。代码如下。我错过了什么?

<?php //Gets list of weeks that I can bill
try {
$stmt2 = $db->prepare("SELECT DISTINCT YEAR(transaction_date) AS YR, WEEK(bo_hourly_charges.transaction_date) as wk FROM bo_hourly_charges WHERE billed_by='AfterCare' ORDER BY transaction_date DESC");
$stmt2->execute();
// set the resulting array to associative
$result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
$count = $stmt2->rowCount();
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<div align='center'>

<br>
<?php
foreach (($stmt2) as $row) {
$fix = $row['YR']."W".$row['wk'];
$monday=date('M d',strtotime($fix));
$friday=date('M d',strtotime($fix.'5'));
print $fix." ".$row['wk']." ".$monday."-".$friday."<br>";
++$i;
}
?>

最佳答案

有趣的问题,我不知道 PHP 允许这种格式。我不相信这是记录在案的行为。因此,依赖它可能并不明智。

Running your code with the full range of weeks in 2017快速显示问题与 2018 年无关:

<?php

$dates = [];
for ($i = 1; $i <= 52; $i++) {
$dates[] = ['YR' => 2017, 'wk' => $i];
}

foreach ($dates as $row) {
$fix = $row['YR']."W".$row['wk'];
$monday=date('M d',strtotime($fix));
$friday=date('M d',strtotime($fix.'5'));
print $fix." ".$row['wk']." ".$monday."-".$friday."\n";
++$i;
}

产量:

(...)
2017W5 5 Dec 31-Dec 31
2017W6 6 Dec 31-Dec 31
2017W7 7 Dec 31-Dec 31
2017W8 8 Dec 31-Dec 31
2017W9 9 Dec 31-Dec 31
2017W10 10 Mar 06-Mar 10
2017W11 11 Mar 13-Mar 17
2017W12 12 Mar 20-Mar 24
2017W13 13 Mar 27-Mar 31
2017W14 14 Apr 03-Apr 07
(...)

相反,2017 年的前九周似乎也有类似的问题。从第 10 周开始,问题似乎已解决。

第 1 至 9 周与第 10 周及以上的差异与格式密切相关。

Padding the weeks with a zero似乎解决了问题:

<?php

$dates = [];
for ($i = 1; $i <= 52; $i++) {
$dates[] = ['YR' => 2017, 'wk' => $i];
}

foreach ($dates as $row) {
$fix = $row['YR']."W".str_pad($row['wk'], 2, '0', STR_PAD_LEFT);
$monday=date('M d',strtotime($fix));
$friday=date('M d',strtotime($fix.'5'));
print $fix." ".$row['wk']." ".$monday."-".$friday."\n";
++$i;
}

然而,更明智的选择是使用更常见的日期格式。

关于php - 确定星期一和星期五代码停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48651840/

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