【发布时间】:2018-02-06 20:58:01
【问题描述】:
我有一个数据库中的条目,我想按它们发生的星期对其进行分组。我要做的是确定条目发生在一年中的哪一周,然后显示该周以及该周的星期一和星期五。此页面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;
}
?>
【问题讨论】:
-
可能是
strtotime($fix),因为我无法在此处提交对 yyyyWnn 格式的任何引用:php.net/manual/en/datetime.formats.date.php 但这只是一个疯狂的猜测。哦,停下,我找到了! :-) 它是一种复合格式。它建议在星期五的 5 点之前使用-(破折号),也许是这样?当它必须是 15 和 1-5 时,我可能无法区分 15 和 15。显然我不知道......对不起。