问题
I am looking for a library (open source) like Joda-Time in the Java world. Is there any library like that?
Joda-Time is very helpful to calculate date and time. I can add days, weeks, month, year and also can converting date and time easily.
I wish there is library like Joda-Time for PHP.
Edit: I need some functions that available in Joda-Time like daysBetween (to calculate number of days between 2 date), monthsBetween and weeksBetween ... Some functions about add and substract date is available from PHP itself.
回答1:
This is what you are looking for : https://github.com/briannesbitt/Carbon
回答2:
I'm not familiar with Joda, but did you took a look into DateTime? It does all the things (and some more) that you've mentioned.
回答3:
strtotime and date can sometimes do wonders, especially if you are working with dates >= 1970 (ie, which can be coded as a UNIX timestamp).
If you are using a recent enough version of PHP, the DateTime class (added in PHP 5.2 ; but several methods were added in PHP 5.3) might be helpful too -- other Date and Time classes could help, too.
回答4:
You can try this library for PHP Date and time http://nuclearscripts.com/php/scripts-and-programs/date-and-time/php-date-library.html
Quoting from there
The PHP Date Library is collection of professional native PHP functions to work with dates. It does not require any PHP extensions. This library includes most useful functions to work with dates. It includes functions to validate the date, shift date by given amount of days, calculate difference between two dates, calculate week number for given date and much more. It properly handles leap years and is ISO compatible. Professional programmer has spent 3+ days to study the subject, code the library, write manual and put it all here.
I havn't tested it personally.
回答5:
I know a component of the Zend Framework: Zend_Date. It works fine for this job.
I am not sure, but you can use it without the whole Framework.
Zend Date
回答6:
There is no need for external library. PHP is more than capable of this already.
<?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
?>
来源:https://stackoverflow.com/questions/1325850/date-and-time-helper-for-php-like-joda-time-in-java