2009-08-25 62 views
9

我正在寻找一个图书馆(开放源码),如在Java世界中的Joda-Time。有没有这样的图书馆?PHP日期和时间助手(如Java中的Joda-Time)

Joda-Time对计算日期和时间非常有帮助。我可以添加日,周,月,年,也可以轻松地转换日期和时间。

我希望像Joda-Time for PHP这样的图书馆。我们需要一些在Joda-Time中可用的函数,比如daysBetween(计算2日期之间的天数),monthsBetween和weeksBetween ... 关于添加和减少日期的一些函数可以从PHP本身获得。

回答

0

我知道Zend Framework的一个组件:Zend_Date。这项工作很好。

我不确定,但您可以在没有整个框架的情况下使用它。

Zend Date

0

我不熟悉乔达,但是你拍了一下到DateTime?它完成了你所提到的所有事情(以及更多)。

0

strtotimedate有时可以做奇观,尤其是如果使用的日期> = 1970 (即,可以被编码为一个UNIX时间戳)工作。

如果您使用的是最新版本的PHP,DateTime类(在PHP 5.2中添加;但在PHP 5.3中添加了几种方法)可能也有帮助 - 其他Date and Time类也可以提供帮助。

+0

谢谢,我只知道strtotime。:) strtotime很好,但没有像daysBetween,weeksBetween,monthsBetween之类的函数。约达有我需要扮演的日期。 – nightingale2k1 2009-08-25 08:59:16

-1

不需要外部库。 PHP已经胜任了这一切。

<?php 
/** These examples work with the current time **/ 
echo strtotime("now"), "\n"; 
echo strtotime("10 September 2000"), "\n"; 
echo strtotime("+1 day"), "\n"; 
echo strtotime("+1 week"), "\n"; 
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n"; 
echo strtotime("next Thursday"), "\n"; 
echo strtotime("last Monday"), "\n"; 

/** This is a made up time **/ 
$lessMonth = strtotime("06/19/1986 3:00PM") 
$lessMonth = strtotime("-1 month", $lessMonth); 

echo $lessMonth, "\n"; 
echo gmdate('c', $lessMonth); 

/** 86 400 seconds in a day */ 
$daysBetween = (strtotime("now") - $lessMonth)/86400 
?> 
+16

比较标准的PHP日期函数和Jodatime似乎表明你没有花太多时间与Jodatime ...... ;-) – Guillaume 2012-04-21 18:02:37

+2

这显示了你不应该做什么!硬编码秒是绝对错误的。这没有考虑到:闰年,夏令时,时区,甚至闰秒!不要这样做。 – MikeWallaceDev 2015-12-12 17:23:20

0

你可以试试这个库PHP的日期和时间 http://nuclearscripts.com/php/scripts-and-programs/date-and-time/php-date-library.html

从那里

PHP的日期库报价是 专业的PHP函数集合的日期 工作。它不需要 任何PHP扩展。这个库 包含最有用的功能,以 与日期。它包括功能 验证日期,移动日期由给定的 天数,计算两个日期之间的差异 ,计算给定日期和更多的一周的 号码。 它适当地处理闰年,并且是与ISO兼容的 。专业 程序员花了3天以上的时间学习 的主题,编码库,编写 手册,并把它放在这里。

我没有亲自测试过。