在 PHP 的国际化组件中,还有一个我们并不是很常用的跟日期相关的操作类,它就是日历操作类.说是日历,其实大部分还是对日期时间的操作,一般也是主要用于日期的格式化和比较之类的.但是通常我们直接使用 date 相关的函数或者 DateTime 相关的类操作日期相关的功能,反而比这套日历的功能更方便灵活.当然,本着学习的目的,我们还是来简单地了解一下.
首先还是从格式化时间说起.
IntlCalendar 类的 createInstance() 方法会返回一个 IntlCalendar 对象,它的参数是可选的,不过必须是 TimeZone 类型的参数.fromDateTime() 方法同样也是生成一个 IntlCalendar 对象,不过它可以设置一个 DateTime 对象或者日期类型的字符串为参数.
可以看到,我们返回的对象使用 get_class() 方法后看到实际返回的是一个 IntlGregorianCalendar 格林格里日历对象.这时,就可以使用 IntlDateFormatter 类的 formatObject() 方法来格式化输出内容,它是可以指定地区的,不同的地区设置就会显示不同的格式化语言结果.
不多做解释了,不过这个静态方法返的是带毫秒数的时间戳.
只要是国际化相关的功能,都多少和时区 TimeZone 有关,日历类也不例外.
使用 getTimeZone() 就可以获得当前的时区信息,getLocale() 和之前我们文章中其它相关功能类的 getLocale() 方法没有什么区别,大家可以看下之前讲过的内容.当然,这个 TimeZone 属性除了通过 ini_set() 之外,也是可以直接通过对象的 setTimeZone() 方法进行修改的.
这是什么意思呢?先看下代码.
这个功能主要是可以设置一周的起始日期是周几.比如对于欧美的国际标准时间来说,周一并不是一周的开始,周日才是这一周的第一天.大家从各种日历应用中就能发现这个问题.
这个比较简单,日历对象内部的属性不同,当然 equals() 方法返回的结果就是 false 了.
除了比较日历对象外,还可以获取两个日历时间之前的差值信息.
可以看到使用 fieldDifference() 方法就可以获得日历对象和比较日期之间相关的信息.需要注意的是,使用 fieldDifference() 之后,原来的日历对象全变成新的日期信息.
getKeywordValuesForLocale() 方法的第一个参数只能固定写 calendar ,后面是填写相关的区域,返回的内容就是当前语言环境下所支持的相关字值信息.
很明显,getType() 方法返回的就是指定语言区域信息的类型.
使用 roll() 方法可以滚动或者说是卷动日历,今天这一节我们将日历滚动一天,也就是加了一天的时间.
使用 toDateTime() 方法就可以将当前的 IntlCalendar 对象转换成 DateTime 对象.
getAvailableLocales() 返回的是当前系统中所有支持可用的 Locale 信息.
关于日历类其实还有很多方法函数,但是看得人非常头晕,英文解释不多,资料也不清晰,所以这里就是简单的列举了一些内容.大家还是报以学习的心态了解即可,当需要使用到的时候可以快速地想起还这些功能就可以了.
测试代码:中的国际化日历类.php
参考文档:
php
function cal_days_in_year($year){
$days=0;
$days = $days ◆ cal_days_in_month(CAL_GREGORIAN,$month,$year);
}
return $days;
//闰年
echo "\n";
//平年
//此时此刻呢我们是用php的内置函数cal_days_in_month()
我把我写的分享给你吧
/**
*?显示日历
*?@param?int?$time?时间戳
*/
private?function?__calendarPanel($time?=?null)?{
$time?||?$time?=?time();
$dateinfo?=?getdate($time);
$calendar?=?array(
'year'?=?$dateinfo['year'],
'month'?=?$dateinfo['mon'],
'day'?=?$dateinfo['mday'],
);
$m_start?=?strtotime(date('Y-m-01',?$time));?//本月第一天
$m_start_w?=?get_week($m_start,?true);?//本月第一天星期索引,0表示星期日
$m_end_w?=?get_week($m_end,?true);?//本月最后一天星期索引,0表示星期日
//补齐上月日期
for?($i?=?0;?$i?$m_start_w;?$i◆◆)?{
$calendar['days'][]?=?array(
'style'?=?'bef_month',
//本月日期
'style'?=?'the_month'?.?(date('d',?$i)?==?$calendar['day']?"?bold"?:?""),
'day'?=?abs(date('d',?$i)),
//补齐下月日期
'style'?=?'aft_month',
return?$calendar;
calendar.class.php
代码如下:
class?Calendar?{
private?$year;?//当前的年
private?$month;?//当前的月
private?$start_weekday;?//当月的第一天对应的是周几
private?$days;?//当前月一共多少天
function?__construct(){
$this-year=isset($_GET["year"])?$_GET["year"]?:?date("Y");
$this-month=isset($_GET["month"])?$_GET["month"]?:?date("m");
$this-start_weekday=date("w",?mktime(0,?0,?0,?$this-month,?1,?$this-year));
$this-days=date("t",?mktime(0,?0,?0,?$this-month,?1,?$this-year));
function?out(){
echo?'table?align="center"';
$this-chageDate("test.php");
$this-weeksList();
$this-daysList();
echo?'/table';
private?function?weeksList(){
$week=array('日','一','二','三','四','五','六');
echo?'tr';
for($i=0;?$icount($week);?$i◆◆)
echo?'th?class="fontb"'.$week[$i].'/th';
echo?'/tr';
private?function?daysList(){
//输出空格(当前一月第一天前面要空出来)
for($j=0;?$j$this-start_weekday;?$j◆◆)
echo?'td?/td';
for($k=1;?$k=$this-days;?$k◆◆){
$j◆◆;
if($k==date('d'))
echo?'td?class="fontb"'.$k.'/td';
else
echo?'td'.$k.'/td';
echo?'/trtr';
//后面几个空格
private?function?prevYear($year,?$month){
$year=$year-1;
return?"year={$year}month={$month}";?
private?function?prevMonth($year,?$month){
if($month?==?1)?{
$year?=?$year?-1;
}else{
$month--;
private?function?nextYear($year,?$month){
$year?=?$year?◆?1;
private?function?nextMonth($year,?$month){
$year◆◆;
$month=1;
$month◆◆;
private?function?chageDate($url=""){
echo?'tda?href="?'.$this-prevYear($this-year,?$this-month).'"'.''.'/a/td';
echo?'tda?href="?'.$this-prevMonth($this-year,?$this-month).'"'.''.'/a/td';
echo?'form';
echo?'select?name="year"?onchange="window.location=''.$url.'?year='◆this.options[selectedIndex].value◆'month='.$this-month.''"';
$selected?=?($sy==$this-year)?"selected"?:?"";
echo?'option?'.$selected.'?value="'.$sy.'"'.$sy.'/option';
echo?'/select';
echo?'select?name="month"?onchange="window.location=''.$url.'?year='.$this-year.'month='◆this.options[selectedIndex].value"';
$selected1?=?($sm==$this-month)?"selected"?:?"";
echo?'option?'.$selected1.'?value="'.$sm.'"'.$sm.'/option';
echo?'/form';?
echo?'/td';
echo?'tda?href="?'.$this-nextYear($this-year,?$this-month).'"'.''.'/a/td';
echo?'tda?href="?'.$this-nextMonth($this-year,?$this-month).'"'.''.'/a/td';
test.php
style
table?{
.fontb?{
color:white;
background:blue;
th?{
td,th?{
text-align:center;
form?{
margin:0px;
padding:0px;
/style
include?"calendar.class.php";
$calendar=new?Calendar;
$calendar-out();
以上就是土嘎嘎小编为大家整理的php输出日历相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!