非常全面的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() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:输出: 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还有多少天: 第二种 孩子的成长 echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';
$stime='2005-11-03 10:08'; function weeksofnow($stime) function daysofnow($stime) function monthsofnow($stime) function yearsofnow($stime) // 下面的函数只是加空格用的,不是核心内容,只为美观 第三种情况:明天,下个月和明年的日期,就可以用以下的代码: echo $tomorrow.'
'; echo $nextmonth.' '; echo $nextyear.' '; 第四种情况:工作时间(刨除假日) $holidayArr=array("05-01","05-02","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); for($n=$beginUX;$n<=$endUX;$n=$n+86400){ 第五种情况:给出秒算小时 $output = '';
$hours = floor($sec / 3600); $minutes = floor($remainSeconds / 60); if($sec >= 3600){ if($sec >= 60 && $sec < 3600){ return $output .= $seconds.' s '; echo transform(3231803); ?> 以上就是为大家提供的php日期时间运算全部实例,希望对大家的学习有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |