Home  >  Article  >  Backend Development  >  Detailed explanation of PHP week time code

Detailed explanation of PHP week time code

小云云
小云云Original
2018-03-07 09:10:281742browse

This article mainly shares with you the detailed explanation of PHP weekly time code, hoping to help everyone.

/*
系统自带的date('W',time()) 有bug  比如,一年中的第一天如果不是周一的话,返回值是
52或者53。 可以实测一下。就是说, 系统默认认为这一天是去年的第52/53周。
*/
 
echo get_weeks_num('2017-01-01');
 
function get_weeks_num($time){
        $month = intval(date('m',$time));//当前时间的月份
        $fyear = strtotime(date('Y-01-01',$time));//今年第一天时间戳
        $fdate = intval(date('N',$fyear));//今年第一天 周几
        $sysweek = intval(date('W',$time));//系统时间的第几周
        //大于等于52 且 当前月为1时, 返回1
        if(($sysweek >= 52 && $month == 1)){
            return 1;
        }elseif($fdate == 1){
            //如果今年的第一天是周一,返回系统时间第几周
            return $sysweek;
        }else{
            //返回系统周+1
            return $sysweek + 1;
        }
    }
<?php
    $strTime = &#39;2017-09-14&#39;;
    $intWeek = ceil(((strtotime($strTime) - strtotime("2017-01-01 00:00:00")))/(7*86400));
    var_dump($strTime."是今年的第".$intWeek."周");
 
?>
<?PHP
    getdate();

Related recommendations:

PHP timestamp and date conversion example sharing

php time function php converts seconds to duration ( h:i:s format)

php time format processing method

The above is the detailed content of Detailed explanation of PHP week time code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn