【问题标题】:How to get timespan values Codeigniter如何获取时间跨度值 Codeigniter
【发布时间】:2015-10-02 01:18:56
【问题描述】:

我正在尝试从 Code Igniter 中的 timespan() 函数中获取值。我有一个问题,我怎么知道是否显示了每种日期格式?例如,对于下面的代码,一个时间跨度可能是 2 Months, 6 Days, 13 Hours, 14 Minutes,它有 4 个元素,而另一个时间跨度可能是 2 Months, 1 Week, 3 Days, 4 Hours, 39 Minutes,它有 5 个元素。如果使用preg_replace,那么我将不知道是几个月、几周或几天等。

那么我如何确定是星期还是几天或几小时等?因为我想将这些值转换为像 39mins/1440mins 这样的周期将是 0.0270833 天,所以我将计算小时数天数周数月和年并将它们加在一起

// Hold Cycle Time Conversion

foreach($results as $key => $values)
{
    // Convert mysql timestamp to unix
    $remove = array('-', ':', ' ');
    $hold_timestamp = mysql_to_unix( str_replace( $remove, '', $values['hold_date'] ) );

    // Get timespan
    $hold_timestamp = timespan($hold_timestamp);

    // Explode into arrays
    $timestamp_format = explode(',', $hold_timestamp);

    // Check each array to get value
    foreach($timestamp_format as $ts)
    {
        $separated_stamp = preg_replace('/[^0-9]/', '', $ts);

        /*** Stuck here, incomplete ***/
    }

}

【问题讨论】:

    标签: php codeigniter timestamp unix-timestamp timespan


    【解决方案1】:

    我决定放弃这种逻辑并改用 PHP 的 Date Time class。从this post 也得到了一些帮助。不确定这是否是最好的方法,但这是我发现的。

    // Hold Cycle Time Conversion
    
    foreach($results as $key => $values)
    {
        // Declare timestamps
        $last = new DateTime( $values['hold_date'] );
        $now = new DateTime( date( 'Y-m-d h:i:s', time() )) ; 
    
        // Find difference
        $interval = $last->diff($now);
    
        // Store in variable to be used for calculation etc
        $years = (int)$interval->format('%Y');
        $months = (int)$interval->format('%m');
        $days = (int)$interval->format('%d');
        $hours = (int)$interval->format('%H');
        $minutes = (int)$interval->format('%i');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 2012-08-16
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多