Php 计算两个日期之间的时差,并给出如下答案;2天3小时前”;
是否有任何公开使用php编写的类/函数会使用时间戳,并以天、月、年等为单位返回此后经过的时间?基本上,我想要相同的功能,生成发布后的时间,并与此网站(以及digg和其他网站的负载)上的每个条目一起呈现.这是作为wordpress插件编写的,但您可以提取相关的PHP代码。没问题:我不确定是否会有这样的类,但我在Google上找到了一些方法来实现您想要的:Php 计算两个日期之间的时差,并给出如下答案;2天3小时前”;,php,date,Php,Date,是否有任何公开使用php编写的类/函数会使用时间戳,并以天、月、年等为单位返回此后经过的时间?基本上,我想要相同的功能,生成发布后的时间,并与此网站(以及digg和其他网站的负载)上的每个条目一起呈现.这是作为wordpress插件编写的,但您可以提取相关的PHP代码。没问题:我不确定是否会有这样的类,但我在Google上找到了一些方法来实现您想要的: 可能其中一个适合或需要,或者您可以轻松地进行调整。这是我编写的Zend Framework ViewHelper,您可以轻松地修改它,
可能其中一个适合或需要,或者您可以轻松地进行调整。这是我编写的Zend Framework ViewHelper,您可以轻松地修改它,以不使用ZF特定的代码:
/**
* @category View_Helper
* @package Custom_View_Helper
* @author Chris Jones <leeked@gmail.com>
* @license New BSD License
*/
class Custom_View_Helper_HumaneDate extends Zend_View_Helper_Abstract
{
/**
* Various time formats
*/
private static $_time_formats = array(
array(60, 'just now'),
array(90, '1 minute'), // 60*1.5
array(3600, 'minutes', 60), // 60*60, 60
array(5400, '1 hour'), // 60*60*1.5
array(86400, 'hours', 3600), // 60*60*24, 60*60
array(129600, '1 day'), // 60*60*24*1.5
array(604800, 'days', 86400), // 60*60*24*7, 60*60*24
array(907200, '1 week'), // 60*60*24*7*1.5
array(2628000, 'weeks', 604800), // 60*60*24*(365/12), 60*60*24*7
array(3942000, '1 month'), // 60*60*24*(365/12)*1.5
array(31536000, 'months', 2628000), // 60*60*24*365, 60*60*24*(365/12)
array(47304000, '1 year'), // 60*60*24*365*1.5
array(3153600000, 'years', 31536000), // 60*60*24*365*100, 60*60*24*365
);
/**
* Convert date into a pretty 'human' form
* Now with microformats!
*
* @param string|Zend_Date $date_from Date to convert
* @return string
*/
public function humaneDate($date_from)
{
$date_to = new Zend_Date(null, Zend_Date::ISO_8601);
if (!($date_from instanceof Zend_Date)) {
$date_from = new Zend_Date($date_from, Zend_Date::ISO_8601);
}
$dateTo = $date_to->getTimestamp(); // UnixTimestamp
$dateFrom = $date_from->getTimestamp(); // UnixTimestamp
$difference = $dateTo - $dateFrom;
$message = '';
if ($dateFrom <= 0) {
$message = 'a long time ago';
} else {
foreach (self::$_time_formats as $format) {
if ($difference < $format[0]) {
if (count($format) == 2) {
$message = $format[1] . ($format[0] === 60 ? '' : ' ago');
break;
} else {
$message = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
break;
}
}
}
}
return sprintf('<abbr title="%sZ">%s</abbr>',
$date_from->get('YYYY-MM-ddTHH:mm:ss'),
$message
);
}
}
/**
*@category-View\u-Helper
*@package Custom\u View\u Helper
*@作者克里斯·琼斯
*@license新的BSD许可证
*/
类Custom\u View\u Helper\u HumaneDate扩展了Zend\u View\u Helper\u抽象
{
/**
*各种时间格式
*/
私有静态$\u时间\u格式=数组(
数组(60,'刚才'),
数组(90,'1分钟'),//60*1.5
数组(3600,'分钟',60),//60*60,60
阵列(5400,'1小时'),//60*60*1.5
阵列(86400,'小时',3600),//60*60*24,60*60
阵列(129600,'1天'),//60*60*24*1.5
阵列(604800,'天',86400),//60*60*24*7,60*60*24
阵列(907200,'1周'),//60*60*24*7*1.5
阵列(2628000,'周',604800),//60*60*24*(365/12),60*60*24*7
数组(3942000,'1个月'),//60*60*24*(365/12)*1.5
阵列(31536000,'月',2628000),//60*60*24*365,60*60*24*(365/12)
阵列(47304000,'1年'),//60*60*24*365*1.5
数组(3153600000,'年',31536000),//60*60*24*365*100,60*60*24*365
);
/**
*将日期转换为漂亮的“人类”形式
*现在使用微格式!
*
*@param string | Zend_Date$Date_from Date to convert
*@返回字符串
*/
公共功能humaneDate($date\u from)
{
$date_to=新的Zend_日期(null,Zend_日期::ISO_8601);
如果(!($date\u从Zend\u日期的实例算起)){
$date_from=新的Zend_日期($date_from,Zend_date::ISO_8601);
}
$dateTo=$date\u to->getTimestamp();//unixtTimeStamp
$dateFrom=$date_from->getTimestamp();//UnixtTimeStamp
$difference=$dateTo-$dateFrom;
$message='';
如果($DATEFORM get('YYYY-MM-ddTHH:MM:ss'),
$message
);
}
}
谷歌兄弟知道答案:
这是以前提出的问题:
这是我见过的最好的版本(用于可读格式):
PHP 5+现在内置了一些功能:
我个人一直在寻找计算天数(作为小数点)的方法,这样我就可以将其分为几年,等等
function daysDifference($d1,$d2)
{
$ts2 = strtotime($d1);
$ts1 = strtotime($d2);
$seconds = abs($ts2 - $ts1); # difference will always be positive
$days = $seconds/60/60/24;
return $days;
}
此函数返回一个数值数组。您可以提取年、月、日、时、分和秒。e、 g.echo$result[3]为您计算小时,echo$result[4]为您计算分钟。(我借用了这个代码)。干杯
你能翻译一下吗?可能是重复的,或者提到的链接已经过期了
function dateDiff($time1, $time2, $precision = 6)
{
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year', 'month', 'day', 'hour', 'minute', 'second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value >= 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value; // . " " . $interval;
$count++;
}
}
// Return string with times
//return implode(", ", $times);
return $times;
}