Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-从日期获取周数_Php_Strtotime - Fatal编程技术网

PHP-从日期获取周数

PHP-从日期获取周数,php,strtotime,Php,Strtotime,我有个约会,我想知道那个月的周数,我怎么做 我试过: $time = '2016/05/21'; echo date("w", strtotime($time)); 我也试过使用W,但这是一年中的一周,但就我而言,我需要一个月 当前结果:6 期望结果:4试试这个 function weekOfMonth($date) { //Get the first day of the month. $firstOfMonth = strtotime(date("Y-m-01", $d

我有个约会,我想知道那个月的周数,我怎么做

我试过:

$time = '2016/05/21';
echo date("w", strtotime($time));
我也试过使用
W
,但这是一年中的一周,但就我而言,我需要一个月

当前结果:6

期望结果:4

试试这个

  function weekOfMonth($date) {
    //Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", $date));
    //Apply above formula.
    return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
  }
你可以在这里看到

你必须按要求执行

$datee = "2016/08/10";
$datee = strtotime( str_replace("/", "-", $datee));
echo weekOfMonth($datee);
试试这个

  function weekOfMonth($date) {
    //Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", $date));
    //Apply above formula.
    return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
  }
你可以在这里看到

你必须按要求执行

$datee = "2016/08/10";
$datee = strtotime( str_replace("/", "-", $datee));
echo weekOfMonth($datee);

我在另一本书中找到了相同的解决方案,请看一看

function getWeeks($date, $rollover)
{
    $cut = substr($date, 0, 8);
    $daylen = 86400;

    $timestamp = strtotime($date);
    $first = strtotime($cut . "00");
    $elapsed = ($timestamp - $first) / $daylen;

    $weeks = 1;

    for ($i = 1; $i <= $elapsed; $i++)
    {
        $dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
        $daytimestamp = strtotime($dayfind);

        $day = strtolower(date("l", $daytimestamp));

        if($day == strtolower($rollover))  $weeks ++;
    }

    return $weeks;
}

$time = '2016/05/21';
echo getWeeks($time, 'Sunday'); //4
函数getWeeks($date,$rollover)
{
$cut=substr($date,0,8);
$daylen=86400;
$timestamp=strottime($date);
$first=strottime($cut.00”);
$passed=($timestamp-$first)/$daylen;
$weeks=1;

对于($i=1;$i我在另一个中找到了相同的解决方案,请看一看

function getWeeks($date, $rollover)
{
    $cut = substr($date, 0, 8);
    $daylen = 86400;

    $timestamp = strtotime($date);
    $first = strtotime($cut . "00");
    $elapsed = ($timestamp - $first) / $daylen;

    $weeks = 1;

    for ($i = 1; $i <= $elapsed; $i++)
    {
        $dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
        $daytimestamp = strtotime($dayfind);

        $day = strtolower(date("l", $daytimestamp));

        if($day == strtolower($rollover))  $weeks ++;
    }

    return $weeks;
}

$time = '2016/05/21';
echo getWeeks($time, 'Sunday'); //4
函数getWeeks($date,$rollover)
{
$cut=substr($date,0,8);
$daylen=86400;
$timestamp=strottime($date);
$first=strottime($cut.00”);
$passed=($timestamp-$first)/$daylen;
$weeks=1;

对于($i=1;$i小写字母“w”是一周中的一天,而不是一个月中的周数。这在中有明确说明。您可能是指大写字母“w”,即ISO周数,而不是一个月中的周数。当您说“该月的周数”时,周“1”是什么-如果第一天是星期六,那么该月的第二天是第2周还是第1周?有些人从星期一开始计算,有些人从星期日开始计算,有些人从ISO周数开始计算,等等……你需要澄清第一周是什么。在我的情况下,“星期日”是开始日期。小写字母“w”是一周中的一天,而不是一个月中的周数。这在中有明确说明。您可能是指大写字母“W”,即ISO周数,而不是该月的周数。当您说“该月的周数”时,周“1”是什么-如果第一天是星期六,那么该月的第二天是第2周还是第1周?有些人从星期一开始计算,有些人从星期天开始计算,有些人从ISO周数开始计算,等等……你需要澄清第一周是什么。在我的情况下,“星期天”是开始日。我需要的是月-周,而不是年。@RivnatNasah我已经更新了,请检查它的工作情况,谢谢。这个答案实际上对我有用,而其他人不会-thanks@RozzA不客气。!我需要的是一周一个月,而不是一年。@RivnatNasah我已经更新了,请检查它的工作情况,谢谢你。这个答案实际上对我有用,而其他人不会t-thanks@RozzA不客气。!