从日期 PHP 获取周数(以年为单位)

2022-08-30 08:03:27

我想约会并计算出它的周数。

到目前为止,我有以下内容。它返回 24,而它应该是 42。

<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>

数字颠倒是错误的和巧合吗?还是我快到了?


答案 1

今天,使用PHP的DateTime对象更好:

<?php
$ddate = "2012-10-18";
$date = new DateTime($ddate);
$week = $date->format("W");
echo "Weeknummer: $week";

这是因为 在 中,它是这样的:mktime()

mktime(hour, minute, second, month, day, year);

因此,您的订单是错误的。

<?php
$ddate = "2012-10-18";
$duedt = explode("-", $ddate);
$date  = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week  = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>

答案 2
$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));

推荐