PHP自定义时间函数

1.php取本月本周或者下月下周的开始到结束时间

 

<?php
@date_default_timezone_set('Asia/Chongqing');

$now_time = time();
$date=date("Y-m-d",$now_time);

function get_date($date,$t='d',$n=0)
{
if($t=='d'){
   $firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
   $lastday = date("Y-m-d 23:59:59",strtotime("$n day"));

}elseif($t=='w'){
   if($n!=0){$date = date('Y-m-d',strtotime("$n week"));}
   $lastday = date("Y-m-d 00:00:00",strtotime("$date Sunday"));
   $firstday = date("Y-m-d 23:59:59",strtotime("$lastday -6 days"));

}elseif($t=='m'){
   if($n!=0){$date = date('Y-m-d',strtotime("$n months"));}
   $firstday = date("Y-m-01 00:00:00",strtotime($date));
   $lastday = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day")); 
}
    return array($firstday,$lastday);
}

$day1   = get_date($date,'d');
$day2   = get_date($date,'d',-1);
$week1 = get_date($date,'w');
$week2 = get_date($date,'w',-1);
$month1 = get_date($date,'m');
$month2 = get_date($date,'m',-1);

echo '<pre>';
print_r($day1);//今天
print_r($day2);//昨天
print_r($week1);//这周
print_r($week2);//上周
print_r($month1);//这月
print_r($month2);//上月
echo '</pre>';
?>

最后一个参数$n如果为负数代表之前日期,为正数代表之后日期,比如-3代表三周前或三天前或三月前,3代表三周后或三天后或三月后

以上代码取月份有误

function get_date($t='d',$n=0)
{
   if($this->rq_dt['yesterday']){
    $n = $n-1;
    $lastdayformat = 'Y-m-d 23:59:59';
   }else{
    $date = time();
    $lastdayformat = 'Y-m-d H:i:s';
    $h = date('H',$date);
    $i = date('i',$date);
    $s = date('s',$date);
   }
   if($t=='d'){
    $firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
    $lastday = date($lastdayformat,strtotime("$n day"));

   }elseif($t=='w'){
    $lastday = date($lastdayformat,strtotime("$n week"));
    $firstday = date("Y-m-d 00:00:00",strtotime("$lastday -1 Monday"));

   }elseif($t=='m'){
    $firstday = date('Y-m-d H:i:s',mktime(0,0,0,date("m")+$n,1,date('Y')));
    $lastday = date("Y-m-d $h:$i:$s",mktime(0,0,-1,date("m")+1+$n,1,date('Y')));
   }
   return array($firstday,$lastday);
}

 

2.php unixtime 与 date 转换

 

function makeTime()
    {
        $selectTime = time();
                 return date("Y",$selectTime)."-".date("m",$selectTime)."-".date("d",$selectTime)." ".date("H",$selectTime).":".date("i",$selectTime).":".date("s",$selectTime);
    }

 将当前 unixtime 转换成当前的 年月日时分秒


将任何英文文本的日期时间描述解析为 Unix 时间戳
echo  strtotime("now"), "\n";
echo  strtotime("10 September 2000"), "\n";
echo  strtotime("+1 day"), "\n";
echo  strtotime("+1 week"), "\n";
echo  strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo  strtotime("next Thursday"), "\n";
echo  strtotime("last Monday"), "\n";
echo strtotime("2010-04-16 16:37:12");
 
To use the below, call it like a normal function. There are four arguments. The first determines what you want to measure the date difference in - years, months, quarters, etc - and the allowed values of this are listed in the first few lines of the function. The next two are the dates themselves. Any valid date should work just fine. You can also use timestamps as dates, although if you do, you must set the last of the four arguments to “true”. You can call it like so:
example:
datediff(”m”,’1978-10-11′,date(”Y-m-d”))
echo datediff(’w', ‘9 July 2003′, ‘4 March 2004′, false);
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is “1″, the first day. 2nd Feb 2003 is “33″. The datediff is “-32″.)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/

if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds

switch($interval) {

case ‘yyyy’: // Number of full years

$years_difference = floor($difference / 31536000);
if (mktime(date(”H”, $datefrom), date(”i”, $datefrom), date(”s”, $datefrom), date(”n”, $datefrom), date(”j”, $datefrom), date(”Y”, $datefrom)+$years_difference) > $dateto) {
$years_difference–;
}
if (mktime(date(”H”, $dateto), date(”i”, $dateto), date(”s”, $dateto), date(”n”, $dateto), date(”j”, $dateto), date(”Y”, $dateto)-($years_difference+1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;

case “q”: // Number of full quarters

$quarters_difference = floor($difference / 8035200);
while (mktime(date(”H”, $datefrom), date(”i”, $datefrom), date(”s”, $datefrom), date(”n”, $datefrom)+($quarters_difference*3), date(”j”, $dateto), date(”Y”, $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference–;
$datediff = $quarters_difference;
break;

case “m”: // Number of full months

$months_difference = floor($difference / 2678400);
while (mktime(date(”H”, $datefrom), date(”i”, $datefrom), date(”s”, $datefrom), date(”n”, $datefrom)+($months_difference), date(”j”, $dateto), date(”Y”, $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference–;
$datediff = $months_difference;
break;

case ‘y’: // Difference between day numbers

$datediff = date(”z”, $dateto) - date(”z”, $datefrom);
break;

case “d”: // Number of full days

$datediff = floor($difference / 86400);
break;

case “w”: // Number of full weekdays

$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks
$first_day = date(”w”, $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
if ($odd_days > 7) { // Sunday
$days_remainder–;
}
if ($odd_days > 6) { // Saturday
$days_remainder–;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;

case “ww”: // Number of full weeks

$datediff = floor($difference / 604800);
break;

case “h”: // Number of full hours

$datediff = floor($difference / 3600);
break;

case “n”: // Number of full minutes

$datediff = floor($difference / 60);
break;

default: // Number of full seconds (default)

$datediff = $difference;
break;
}

return $datediff;

}
 
4.php取当年任意一周的起止时间
function getDateOfWeekNum($date, $n)
	{
		$cur_week = date('W', strtotime($date));
		$t = strtotime("+1 Monday $date");
		
		$b = strtotime("+$n week -$cur_week week", $t);
		
		return $b;
	}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值