【问题标题】:Convert time in mysql with php用php在mysql中转换时间
【发布时间】:2016-05-07 21:28:34
【问题描述】:

这是时间在数据库中的显示方式:

2016-05-07 21:18:21

现在,它是这样转换它的:

function time_elapsed_string($datetime) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);

$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;

$string = array(
    'y' => 'year',
    'm' => 'month',
    'w' => 'week',
    'd' => 'day',
    'h' => 'hour',
    'i' => 'minute',
    's' => 'second',
);
foreach ($string as $k => &$v) {
    if ($diff->$k) {
        $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
    } else {
        unset($string[$k]);
    }
}

$string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}

当我这样做时,它会转换一切。但我希望它只显示多少秒、多少分钟、多少小时,如果是 24 小时,那么它是 1 天和 1 天。我希望它以这种格式显示的所有其他内容:F j, Y g:ia

【问题讨论】:

  • 你能详细说明一下吗? “只显示数字 1,而不是 2 周……”是什么意思?另外,您尝试过什么?
  • 我刚刚编辑了操作。对此感到抱歉
  • 我想我有个主意。如果你在几天内得到差异。 if(diff>=1){ echo "one day"} esleif(diff>=7){ echo "one week" } esleif(diff>=30){ echo "one month" }....在这里但试一试,不想让它成为答案,因为我不确定这是你想要的
  • 等等... if 需要反过来。首先与 30 比较,然后是 7,然后是 1。否则 1 将始终为真
  • @Andreas 是的,你有这个想法,但我希望它只显示多少秒、多少分钟、多少小时,如果是 24 小时,那么它是 1 天和 1 天。我希望它以这种格式显示的所有其他内容:F j, Y g:ia

标签: php mysql date time


【解决方案1】:
/**
 * Format a unix timestamp in a 'time ago' format,
 * e.g. 15 minutes ago or 7 days ago.
 *
 * @param  integer $time
 * @return string
 */
function ago($time)
{
    $config = array(
        array('second',  'seconds'),
        array('minute',  'minutes'),
        array('hour',    'hours'),
        array('day',     'days'),
        array('week',    'weeks'),
        array('month',   'months'),
        array('year',    'years'),
        array('decade',  'decades'),
    );

    list($periods, $lengths, $now) = array($config, [60, 60, 24, 7, 4.35, 12, 10], time());
    $difference = $now - $time;
    for ($j = 0; $difference >= $lengths[$j] and $j < count($lengths) - 1; $j++)
    {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    $period = $difference == 1 ? $periods[$j][0] : $periods[$j][1];
    if ($difference == 0)
    {
        return 'Just now';
    }

    return "${difference} ${period} ago";
}

【讨论】:

    【解决方案2】:
    $old_date = "2016-05-07 21:18:21";
    $date311 = date('d-m-Y H:i:s', strtotime($old_date));
    $date31 = date_create($date311);
    //creating a date object
    date_default_timezone_set("Asia/Calcutta");
    $date411 = date('d-m-Y H:i:s');
    $date41 = date_create($date411);
    //calculating the difference between dates
    //the dates must be provided to the function as date objects that's why we were setting them 
    //as date objects and not just as strings
    //date_diff returns an date object wich can be accessed as seen below
    $diff341 = date_diff($date41, $date31);
    
    //accesing days
    $days1 = $diff341->d;
    //accesing months
    $months1 = $diff341->m;
    //accesing years
    $years1 = $diff341->y;
    //accesing hours
    $hours1=$diff341->h;
    //accesing minutes
    $minutes1=$diff341->i;
    //accesing seconds
    $seconds1=$diff341->s;
    
    echo '<center>';
    echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:600px;font-size:16px">
    <b>The difference between '.$date311.' and '.$date411.' 
    <br />is: ' . $years1 . ' year(s), ' . $months1 . ' month(s), '. $days1 . ' day(s), '.$hours1.' hour(s),
    '.$minutes1.' minute(s), '.$seconds1.' second(s) </b>
    </div><br />';
    echo '</center>';
    

    结果:-

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2011-06-20
      • 2014-09-03
      • 2017-05-13
      相关资源
      最近更新 更多