本文介绍了在几天前更改PHP日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Joumla上使用一个组件,该组件以以下格式返回日期:2个月零6天。
但是我需要这样显示日期:
xx秒前
或xx分钟前
或xx小时前
或xx天前。

I am using a component on Joumla that returns dates in this format: 2 months and 6 days ago. But I need the date to be displayed like this:xx seconds agoor xx minutes agoor xx hours agoor xx days ago.

开发人员告诉我,我必须修改此代码,但我对Php完全陌生。今天我整天连续尝试了12个多小时,而我成功完成的工作就是破坏我的网站。

The developer told me I have to modify this code, but I am completely new to Php. I tried all day today for over 12 hours in a row, and all I was successfully accomplish was to breakdown my site.

所以有人可以帮我这个忙。如果需要,我愿意授予FTP访问权限。这是代码:

So Could anyone give me help with this. I am willing to grant FTP access if is need. this is the code:

function dateFormatFromTo($from, $to = null)
 {
    $par = JComponentHelper::getParams( 'com_djclassifieds' );      
    $to = (($to === null) ? (time()) : ($to));
    $to = ((is_int($to)) ? ($to) : (strtotime($to)));
    $from = ((is_int($from)) ? ($from) : (strtotime($from)));
    $output = '';     
    $limit = $par->get('date_format_ago_limit','2');
    $units = array
    (
       "COM_DJCLASSIFIEDS_DATE_YEAR"   => 29030400, 
       "COM_DJCLASSIFIEDS_DATE_MONTH"  => 2419200,  
       "COM_DJCLASSIFIEDS_DATE_WEEK"   => 604800,   
       "COM_DJCLASSIFIEDS_DATE_DAY"    => 86400,    
       "COM_DJCLASSIFIEDS_DATE_HOUR"   => 3600,     
       "COM_DJCLASSIFIEDS_DATE_MINUTE" => 60,       
       "COM_DJCLASSIFIEDS_DATE_SECOND" => 1         
    );

    $diff = abs($from - $to);
    $suffix = (($from > $to) ? (JTEXT::_('COM_DJCLASSIFIEDS_DATE_FROM_NOW')) : (JTEXT::_('COM_DJCLASSIFIEDS_DATE_AGO')));

    $i=0;
        foreach($units as $unit => $mult){
            if($diff >= $mult){
                if($i==$limit-1 && $i>0){
                    $output .= " ".JTEXT::_('COM_DJCLASSIFIEDS_DATE_AND').' '.intval($diff / $mult)." ";
                }else{
                    $output .= ", ".intval($diff / $mult)." ";
                }   
                //$and = (($mult != 1) ? ("") : (JTEXT::_('COM_DJCLASSIFIEDS_DATE_AND')));
                //$output .= ", ".$and.intval($diff / $mult)." ";
                if(intval($diff / $mult) == 1){
                    $output .= JTEXT::_($unit); 
                }else{
                    $output .= JTEXT::_($unit."S");
                }

                $diff -= intval($diff / $mult) * $mult;
                $i++;
                if($i==$limit){ break; }            
            }
        }
        $output .= " ".$suffix;
        $output = substr($output, strlen(", "));
  return $output;
 }

因此,如果有人可以帮助我,我将不胜感激。如果需要,我可以授予FTP访问权限。
我有PHP 5.3

So, I would greatly appreciate if someone can help me out. I can give FTP access if needed. I have Php 5.3

谢谢

推荐答案

此是类的改编

    date_default_timezone_set('GMT');

    function getDifference($timestamp, $reference = null) {
        static $MINUTE = 60;
        static $HOUR   = 3600;    //static::$MINUTE * 60
        static $DAY    = 86400;   //static::$HOUR * 24
        static $WEEK   = 604800;  //static::$DAY * 7
        static $MONTH  = 2628000; //static::$YEAR / 12
        static $YEAR   = 31536000;//static::$DAY * 365

        $formats = array(
            array(0.7 * $MINUTE, '%d seconds ago', 1),
            array(1.5 * $MINUTE, 'a minute ago',   1),
            array( 60 * $MINUTE, '%d minutes ago', $MINUTE),
            array(1.5 * $HOUR,   'an hour ago',    1),
            array(      $DAY,    '%d hours ago',   $HOUR),
            array(  2 * $DAY,    'yesterday',      1),
            array(  7 * $DAY,    '%d days ago',    $DAY),
            array(1.5 * $WEEK,   'a week ago',     1),
            array(      $MONTH,  '%d weeks ago',   $WEEK),
            array(1.5 * $MONTH,  'a month ago',    1),
            array(      $YEAR,   '%d months ago',  $MONTH),
            array(1.5 * $YEAR,   'a year ago',     1),
            array(PHP_INT_MAX,   '%d years ago',   $YEAR)
        );

        $futureformats = array(
            array(0.7 * $MINUTE, 'in a moment',   -1),
            array(1.5 * $MINUTE, 'in a minute',   -1),
            array( 60 * $MINUTE, 'in %d minutes', -$MINUTE),
            array(1.5 * $HOUR,   'in an hour',    -1),
            array(      $DAY,    'in %d hours',   -$HOUR),
            array(  2 * $DAY,    'tomorrow',      -1),
            array(  7 * $DAY,    'in %d days',    -$DAY),
            array(1.5 * $WEEK,   'in a week',     -1),
            array(      $MONTH,  'in %d weeks',   -$WEEK),
            array(1.5 * $MONTH,  'in a month',    -1),
            array(      $YEAR,   'in %d months',  -$MONTH),
            array(1.5 * $YEAR,   'in a year',     -1),
            array(PHP_INT_MAX,   'in %d years',   -$YEAR)
        );


        if ($reference === null) {
            $reference = time();
        }

        $timestamp = ($timestamp instanceof DateTime) ? $timestamp->getTimestamp() : (is_numeric($timestamp) ? (int)$timestamp : strtotime($timestamp));
        $reference = ($reference instanceof DateTime) ? $reference->getTimestamp() : (is_numeric($reference) ? (int)$reference : strtotime($reference));

        $delta = $reference - $timestamp;

        if ($delta >= 0) {
            foreach ($formats as $format) {
                if ($delta < $format[0]) {
                    return sprintf($format[1], round($delta / $format[2]));
                }
            };
        } else {
            foreach ($futureformats as $format) {
                if (-$delta < $format[0]) {
                    return sprintf($format[1], round($delta / $format[2]));
                }
            };

        }
    }

    // will output 52 minutes ago
    echo getDifference('2014-01-15 15:24:00', '2014-01-15 16:16:12') . PHP_EOL;

    // without second param will output difference with the current time
    echo getDifference('2014-01-15 15:24:00') . PHP_EOL;

这篇关于在几天前更改PHP日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:54