PHP日期时间核心函数介绍
实例讲解之前,先来介绍几个核心函数: 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() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:
<?php
echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,99)));
?>
输出示例如下:
Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999
strtotime函数。该函数能够将任何英文文本描述的日期时间解析为Unix时间戳。其语法为strtotime(time,now),其中time参数为必需,表示待解析的时间字符串;now参数可选,用于提供计算基准时间,若省略则默认使用当前时间。常用用法包括:strtotime("+1 week")获取一周后的时间,strtotime("-1 week")获取一周前的时间,strtotime("+1 months")获取一月后的时间,strtotime("+1 days")获取一天后的时间,以及strtotime(" +30 seconds ")、strtotime( " +20 minutes ")和strtotime( " +12 hours ")分别获取30秒、20分钟和12小时后的时间。
date函数用于格式化本地时间与日期,语法为date(format,timestamp)。此外,date_default_timezone_set函数可用于设置脚本中所有日期/时间函数使用的默认时区,语法为date_default_timezone_set(timezone)。
以下是具体的应用实例。
第一种情况:在没有数据库支持,仅依靠PHP日期函数进行数值比较的场景下,需要完全依赖PHP内置函数进行计算。
例如,计算从2026年9月5日到2026年9月18日之间相隔的天数。
<?php
$startdate=strtotime("2015-9-5");
$enddate=strtotime("2015-9-18"); //上面的php时间日期函数已经把日期变成了时间戳,就是变成了秒。这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下:
$days=round(($enddate-$startdate)/3600/24) ;
echo $days; //days为得到的天数;
?>
第二种应用场景涉及成长周期的计算。
<?
date_default_timezone_set('Asia/Shanghai');
//以上一句为设置时区,其实不设也行,但是zde debug的时候会有提示,说什么不安全的函数…添上吧。
echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';
$stime='2005-11-03 10:08';
echo "
***自出生($stime)以来…:
";
echo "今天是第".Lnbsp(daysofnow($stime),3)."天
";
echo "今天是第".Lnbsp(weeksofnow($stime),3)."周
";
echo "今天是第".Lnbsp(monthsofnow($stime),3)."个月
";
echo "今天是第".Lnbsp(yearsofnow($stime),3)."年
";
/*
$output=sprintf(" 今天是第%03d天
今天是第< font color=red>%03d周
今天是第< font color=red>%03d个月
今天是第< font color=red>%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.'
';
第四种情况:计算工作时间,即需要扣除假日后的有效工作日。
<?
$startDate="2001-12-12";
$endDate="2002-11-1";
$holidayArr=array("05-01","05-02","10-01","10-01","10-02","10-03","10-04","10-05","01-26","01-27","01-28","01-29");
//假期日期数组,比方国庆,五一,春节等
$endWeek=2;
//周末是否双休.双休为2,仅仅星期天休息为1,没有休息为0
$beginUX=strtotime($startDate);
$endUX=strtotime($endDate);
for($n=$beginUX;$n<=$endUX;$n=$n+86400){
$week=date("w",$n);
$MonDay=date("m-d",$n);
if($endWeek){//去处周末休息
if($endWeek==2){
if($week==0||$week==6) continue;
}
if($endWeek==1){
if($week==0) continue;
}
}
if(in_array($MonDay,$holidayArr)) continue;
$totalHour+=10;//每天工作10小时
}
echo "开始日期:$startDate
";
echo "结束日期:$endDate
";
echo "共花了".$totalHour."小时";
?>
第五种情况:根据给定的秒数换算成对应的小时数。
<?php
function transform($sec){
$output = '';
$hours = floor($sec / 3600);
$remainSeconds = $sec % 3600;
$minutes = floor($remainSeconds / 60);
$seconds = $sec - $hours * 3600 - $minutes * 60;
if($sec >= 3600){
$output .= $hours.' h / ';
$output .= $minutes.' m / ';
}
if($sec >= 60 && $sec < 3600){
$output .= $minutes.' m / ';
}
return $output .= $seconds.' s ';
}
echo transform(3231803);
?>
以上即为PHP日期时间运算的全部实例汇总,希望能对读者的学习有所帮助。