找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1488|回复: 0
打印 上一主题 下一主题

非常全面的php日期时间运算汇总

[复制链接]

2487

主题

2487

帖子

7391

积分

论坛元老

Rank: 8Rank: 8

积分
7391
跳转到指定楼层
楼主
发表于 2018-2-14 05:41:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            实例讲解之前,先来介绍几个核心函数:
mktime 函数
mktime() 函数返回一个日期的 Unix 时间戳。
参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。
参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。
语法:mktime(hour,minute,second,month,day,year,is_dst)
参数               描述  
hour       可选。规定小时。  
minute   可选。规定分钟。  
second   可选。规定秒。  
month    可选。规定用数字表示的月。  
day         可选。规定天。  
year        可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。  
is_dst  可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。   
自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。  
例子:mktime() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:   

输出:   
Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999
strtotime 函数
strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
语法:strtotime(time,now)
参数      描述  
time    规定要解析的时间字符串。  
now     用来计算返回值的时间戳。如果省略该参数,则使用当前时间。   
  一周之后:  strtotime("+1 week") ;
  一周之前:  strtotime("-1 week") ;
  一月之后:  strtotime("+1 months") ;
  一天之后:  strtotime("+1 days") ;      
  30秒之后 strtotime( " +30 seconds " );
  20分钟之后 strtotime( " +20 minutes " );
  12个小时之后 strtotime( " +12 hours " );
date 函数
date() 函数格式化一个本地时间/日期。
语法
date(format,timestamp)
date_default_timezone_set 函数
date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。
date_default_timezone_set(timezone)

实例
第一种情况是没有数据库,只是得到的日期值进行比较的话,那就得完全用php的时间日期函数计算了,如下:
比如要计算2015-9-5到2015-9-18还有多少天:

第二种 孩子的成长
".Lnbsp(daysofnow($stime),3)."
";
echo "今天是第".Lnbsp(weeksofnow($stime),3)."
";
echo "今天是第".Lnbsp(monthsofnow($stime),3)."个月
";
echo "今天是第".Lnbsp(yearsofnow($stime),3)."
";
/*
$output=sprintf(" 今天是第%03d
今天是第%03d
今天是第%03d个月
今天是第%03d
....
".date('w',$ntemp)."
";
return ($ntemp-$ftemp)/60/60/24/7;
}

function daysofnow($stime)
{
$ftime=strtotime($stime);
return ceil(abs((time()-$ftime)/(60*60*24)));
}

function monthsofnow($stime)
{
$ftime=strtotime($stime);
$fmonth=date('m',$ftime);
$fyear=date('Y',$ftime);
$nmonth=date('m');
$nyear=date('Y');
$result=($nyear-$fyear)*12+$nmonth-$fmonth+1;
return $result;
}

function yearsofnow($stime)
{
$ftime=strtotime($stime);
$fyear=date('Y',$ftime);
$nyear=date('Y');
return $nyear-$fyear+1;
}

// 下面的函数只是加空格用的,不是核心内容,只为美观
function Lnbsp($data,$num)
{
$result=trim($data);
for($i=$num;$i>=strlen($data);$i--) {
$result=' '.$result;
}
return $result;
}
?>
第三种 情况:明天,下个月和明年的日期,就可以用以下的代码:
$tomorrow = date('Y-m-d',mktime (0,0,0,date("m"),date("d")+1,date("Y")));
$nextmonth = date('Y-m',mktime (0,0,0,date("m")+1,date("d")+1,date("Y")));
$nextyear = date('Y',mktime (0,0,0,date("m"),date("d"),date("Y")+1));

echo $tomorrow.'
';
echo $nextmonth.'
';
echo $nextyear.'
';
第四种情况:工作时间(刨除假日)


第五种情况:给出秒算小时
= 3600){
$output .= $hours.' h / ';
$output .= $minutes.' m / ';
}

if($sec >= 60 && $sec  
以上就是为大家提供的php日期时间运算全部实例,希望对大家的学习有所帮助。
            
            
您可能感兴趣的文章:
  • PHP strtotime函数详解
  • PHP中strtotime函数使用方法详解
  • php 深入理解strtotime函数的使用详解
  • PHP时间戳 strtotime()使用方法和技巧
  • php使用strtotime和date函数判断日期是否有效代码分享
  • PHP下获取上个月、下个月、本月的日期(strtotime,date)
  • php使用date和strtotime函数输出指定日期的方法
  • PHP使用strtotime计算两个给定日期之间天数的方法
  • PHP使用strtotime获取上个月、下个月、本月的日期
  • php 时间time与日期date之间的使用详解及区别
  • PHP正则匹配日期和时间(时间戳转换)的实例代码
  • PHP实现针对日期,月数,天数,周数,小时,分,秒等的加减运算示例【基于strtotime】
  • PHP中时间加减函数strtotime用法分析
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端