基本方法
下面的章节用例子来说明 只设置一个指定的日期,没有时间部分,那么时间就设置为 00:00:00。相反,只设置一个指定的时间暗示在内部日期 设置为 01.01.1970 加上等同已经发生的小时,分钟和秒的秒数。一般来说,人们从开始点测量东西,如 A.D. 0年。 然而,许多软件系统使用1970年的第一秒作为开始点,并表示为时间戳偏移量。 当前日期
不带任何参数,构造一个实例缺省地返回一个用PHP的 Example #1 生成当前日期
$date = new Zend_Date();
// Output of the current timestamp
print $date;
Zend_Date 的例子
对于不熟悉在其他语言或框架中的日期对象的开发者,回顾一下 输出日期
在 Example #2 get() - 输出日期
$date = new Zend_Date();
// Output of the desired date
print $date->get();
设置日期
Example #3 set() - 设置日期
$date = new Zend_Date();
// Setting of a new time
$date->set('13:00:00',Zend_Date::TIMES);
print $date->get(Zend_Date::W3C);
加减日期
用 Example #4 add() - 加日期
$date = new Zend_Date();
// changes $date by adding 12 hours
$date->add('12:00:00', Zend_Date::TIMES);
echo "Date via get() = ", $date->get(Zend_Date::W3C), "\n";
// use magic __toString() method to call Zend_Date's toString()
echo "Date via toString() = ", $date, "\n";
比较日期
所有基本的 Example #5 compare() - 比较日期
$date = new Zend_Date();
// Comparation of both times
if ($date->compare(10, Zend_Date::MINUTE) == -1) {
print "This hour is less than 10 minutes old";
} else {
print "This hour is at least 10 minutes old";
}
对简单的相等比较,使用 Example #6 equals() - 识别日期或日期部分
$date = new Zend_Date();
// Comparation of the two dates
if ($date->equals(10, Zend_Date::HOUR)) {
print "It's 10 o'clock. Time to get to work.";
} else {
print "It is not 10 o'clock. You can keep sleeping.";
}
|