www.2527.com_澳门新葡8455手机版_新京葡娱乐场网址_
做最好的网站

日数_php才具_脚本之家,总计七个日子那间的间距

2019-11-24 02:18 来源:未知

使用PHP实现总结多个日子区间的年、月、周、日数:复制代码 代码如下:strtotime=array; $start = strtotime; $stop = strtotime; $extend = /86400; $result['extends'] = $extend; if{ //要是小于7天一向回到天数 $result['daily'] = $extend; }elseif{ //小于28天则赶回周数,由于闰年九月满足了 if($stop==strtotime{ $result['monthly'] = 1; }else{ $w = floor; $d = ($stop-strtotime/86400; $result['weekly'] = $w; $result['daily'] = $d; } }else{ $y= floor; if{ //假如超越一年 $start = strtotime; $a = date; //判定是还是不是真正已经有了一年了,若无的话就开减 if{ $a = date('Y-m-d',strtotime; $m =11; $y--; } $extend = /86400; } if{ $w = floor; $d = $extend-$w*7; }else{ $m = isset?$m:round; $stop>=strtotime?$m:$m--; if($stop>=strtotime{ $d=$w=($stop-strtotime/86400; $w = floor; $d = $d-$w*7; } } $result['yearly'] = $y; $result['monthly'] = $m; $result['weekly'] = $w; $result['daily'] = isset?$d:null; } return array_filter; } print_r(format('2012-10-1','2012-12-15'));?>运转结果:Array([extends]=>75[monthly]=>2[weekly]=>2)

例1

据书上说钦点的左右四个日子,总结那多个日子之间顺次周的伊始时间和告竣作时间间,以致种种月的起初时间和得了时间

php 查询某天所在的周数及对应周的开场日期复制代码 代码如下:/*** @file* @version 1.1* @author QQ83989686* @date 二〇一三-8-7 最终修改时间* @brief */ //获取某些日期的 周数、周对应的上马终结时间 private function getWeekStartEndDay { $g = strftime; return array('week_num'=>strftime,'week_start_day'=>strftime('%Y-%m-%d',strtotime,'week_start_day_cn'=>strftime('%Y年%m月%d日',strtotime,'week_end_day'=>strftime('%Y-%m-%d',strtotime,'week_end_day_cn'=>strftime('%Y年%m月%d日',strtotime); }

一贯把日子转变

日志格式化类 Date.class.php

 代码如下

复制代码 代码如下: strftime('%Y-%m-%d', strtotime - $mon_idx * 86400), 'week_end_day' => strftime('%Y-%m-%d', strtotime - $sun_idx * 86400), ); } /** * 依据内定日期获取所在月的开局时间和得了时间 */ public function get_monthinfo_by_date{ $ret = array(); $timestamp = strtotime; $mdays = date; return array( 'month_start_day' => date, 'month_end_day' => date('Y-m-'.$mdays, $timestamp) ); } /** * 获取钦定日期之间的顺序周 */ public function get_weeks { $range_arr = array(); // 检查日期有效性 $this->check_date); // 计算各样周的开场时间 do { $weekinfo = $this->get_weekinfo_by_date; $end_day = $weekinfo['week_end_day']; $start = $this->substr_date($weekinfo['week_start_day']); $end = $this->substr_date($weekinfo['week_end_day']); $range = "{$start} ~ {$end}"; $range_arr[] = $range; $sdate = date('Y-m-d', strtotime; }while; return $range_arr; } /** * 获取钦赐日期之间的相继月 */ public function get_months { $range_arr = array(); do { $monthinfo = $this->get_monthinfo_by_date; $end_day = $monthinfo['month_end_day']; $start = $this->substr_date($monthinfo['month_start_day']); $end = $this->substr_date($monthinfo['month_end_day']); $range = "{$start} ~ {$end}"; $range_arr[] = $range; $sdate = date('Y-m-d', strtotime; }while; return $range_arr; } /** * 截取日期中的月份和日 * @param string $date * @return string $date */ public function substr_date { if return FALSE; return date('m-d', strtotime; } /** * 检查日期的实用 YYYY-mm-dd * @param array $date_arr * @return boolean */ public function check_date { $invalid_date_arr = array(); foreach { $timestamp = strtotime; $standard = date; if $invalid_date_arr[] = $row; } if ( ! empty { die("invalid date -> ".print_r($invalid_date_arr, TRUE)); } } }

function daysbetweendates($date1, $date2){
    $date1 = strtotime($date1);
    $date2 = strtotime($date2);
    $days = ceil(abs($date1 - $date2)/86400);
    return $days;
}

复制代码 代码如下:get_weeks('2014-11-05', '2014-11-29')); /*结果为 array "11-03 ~ 11-09" [1]=> string "11-10 ~ 11-16" [2]=> string "11-17 ~ 11-23" [3]=> string "11-24 ~ 11-30" } */ // 输出2月1日和11月30日 var_dump($datefmt->get_months('2014-02-03', '2014-11-29')); /*结果为 array { [0]=> string "02-01 ~ 02-28" [1]=> string "03-01 ~ 03-31" [2]=> string "04-01 ~ 04-30" [3]=> string "05-01 ~ 05-31" [4]=> string "06-01 ~ 06-30" [5]=> string "07-01 ~ 07-31" [6]=> string "08-01 ~ 08-31" [7]=> string "09-01 ~ 09-30" [8]=> string "10-01 ~ 10-31" [9]=> string "11-01 ~ 11-30" } */

例2

 代码如下

<?php
functionmaketime($date)
{
list($year,$month,$day) = explode('-',$date);
returnmktime(0,0,0,$month,$day,$year);
}
$date1 = '2007-01-08';
$date2 = '2007-03-01';
$d = (maketime($date2) - maketime($date1)) / (3600*24);
echo'相差$d 天';

?>

例3

PHP达成八个日子区间的年、月、周、日数的思索

 

 代码如下

<?php
    function format($a,$b){
        //检查四个日子大小,私下认可前小后大,假如前大后小则调换个地点置以承保前小后大
        if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
        $start  = strtotime($a);
        $stop   = strtotime($b);
        $extend = ($stop-$start)/86400;
        $result['extends'] = $extend;
        if($extend<7){                //若是小于7天一贯重临天数
            $result['daily'] = $extend;
        }elseif($extend<=31){        //小于28天则赶回周数,由于闰年7月满意了
            if($stop==strtotime($a.' 1 month')){
                $result['monthly'] = 1;
            }else{
                $w = floor($extend/7);
                $d = ($stop-strtotime($a.' '.$w.' week'))/86400;
                $result['weekly']  = $w;
                $result['daily']   = $d;
            }
        }else{
            $y=    floor($extend/365);
            if($y>=1){                //假诺抢先一年
                $start = strtotime($a.' '.$y.'year');
                $a     = date('Y-m-d',$start);
                //推断是不是真的已经有了一年了,若无的话就开减
                if($start>$stop){
                    $a = date('Y-m-d',strtotime($a.'-1 month'));
                    $m =11;
                    $y--;   
                }
                $extend = ($stop-strtotime($a))/86400;
            }
            if(isset($m)){
                $w = floor($extend/7);
                $d = $extend-$w*7;
            }else{
                $m = isset($m)?$m:round($extend/30);
                $stop>=strtotime($a.' '.$m.'month')?$m:$m--;
                if($stop>=strtotime($a.' '.$m.'month')){
                    $d=$w=($stop-strtotime($a.' '.$m.'month'))/86400;
                    $w = floor($w/7);
                    $d = $d-$w*7;
                }
            }
            $result['yearly']  = $y;
            $result['monthly'] = $m;
            $result['weekly']  = $w;
            $result['daily']   = isset($d)?$d:null;
        }
        return array_filter($result);
    }
 
    print_r(format('2012-10-1','2012-12-15'));
?>

TAG标签:
版权声明:本文由澳门新葡8455手机版发布于计算机编程,转载请注明出处:日数_php才具_脚本之家,总计七个日子那间的间距