Working 范例
在本章内,我们将描述若干附加的函数,它们在 检查日期
大部分输入的日期可能是字符串,问题是你不能确保这些字符串是真的日期。因此
第三个参数也是可选的并可用于给定一个地方。我们需要地方来格式化月名和天名(monthnames and daynames)。 所以用第三个参数我们可以根据给定的地方识别 '01.Jänner.2000' 或 '01.January.2000'这样的日期。
Example #1 检查日期 // Checking dates $date = '01.03.2000'; if (Zend_Date::isDate($date)) { print "String $date is a date"; } else { print "String $date is NO date"; } // Checking localized dates $date = '01 February 2000'; if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) { print "String $date is a date"; } else { print "String $date is NO date"; } // Checking impossible dates $date = '30 February 2000'; if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) { print "String $date is a date"; } else { print "String $date is NO date"; } 日出和日落
正如很多人不知道他们所居住的城市的位置,我们也使用助手类来提供了世界上大约 250个首都和其它大城市的位置数据。 大多数人使用他们附近的城市来作为地方,这样计算上会差一点时间。
用 Example #2 获得所有可用的城市 // Output the complete list of available cities print_r (Zend_Date_Cities::getCityList());
用
'
当然也可以给出和计算自定义位置,因此 ' Example #3 获得一个城市的位置 // Get the location for a defined city // uses the effective horizon as no horizon is defined print_r (Zend_Date_Cities::City('Vienna')); // use the nautic horizon print_r (Zend_Date_Cities::City('Vienna', 'nautic')); // self definition of a location $mylocation = array('latitude' => 41.5, 'longitude' => 13.2446);
现在所有需要的数据可以被设置了,下一步是创建带有计算日落和日出的日期的 Example #4 计算太阳信息 // Get the location for a defined city $city = Zend_Date_Cities::City('Vienna'); // create a date object for the day for which the sun has to be calculated $date = new Zend_Date('10.03.2007', Zend_Date::ISO_8601, 'de'); // calculate sunset $sunset = $date->getSunset($city); print $sunset->get(Zend_Date::ISO_8601); // calculate all sun informations $info = $date->getSunInfo($city); foreach ($info as $sun) { print "\n" . $sun->get(Zend_Date::ISO_8601); } 时区
时区和日期一样重要,根据用户所居住的地方,有若干时区,所以使用日期意味着要正确设置时区。
这听起来很复杂但其实比较简单。正如在
Example #5 使用时区 // Set a default timezone... this has to be done within the bootstrap // file or php.ini // We do this here just for having a complete example date_default_timezone_set('Europe/Vienna'); // create a date object $date = new Zend_Date('10.03.2007', Zend_Date::DATES, 'de'); // view our date object print $date->getIso(); // what timezone do we have ? print $date->getTimezone(); // set another timezone $date->setTimezone('America/Chicago'); // what timezone do we now have ? print $date->getTimezone(); // see the changed date object print $date->getIso();
如上面例子的第一行所示,
在 Example #6 多重时区 // Set a default timezone... this has to be done within the bootstrap // file or php.ini. // We do this here just for having a complete example. date_default_timezone_set('Europe/Vienna'); // create a date object $date = new Zend_Date('10.03.2007 00:00:00', Zend_Date::ISO_8601, 'de'); // view our date object print $date->getIso(); // the date stays unchanged even after changeing the timezone date_default_timezone_set('America/Chicago'); print $date->getIso(); $otherdate = clone $date; $otherdate->setTimezone('Brazil/Acre'); // view our date object print $otherdate->getIso(); // set the object to the actual systems timezone $lastdate = clone $date; $lastdate->setTimezone(); // view our date object print $lastdate->getIso();
|