设为首页 收藏本站
查看: 296|回复: 0

[经验分享] PHP自定义时间函数

[复制链接]

尚未签到

发表于 2017-3-27 11:26:06 | 显示全部楼层 |阅读模式
  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");


 

3.php datediff 时间间隔函数


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;
}
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-356071-1-1.html 上篇帖子: PHP开发笔记系列(四)-文件操作 下篇帖子: php的ajax分页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表