1

I am trying to get the values from timespan() function in Code Igniter. I have one problem, how can I know whether each of the date format is displayed? For example in regards with the codes below, one timespan might be 2 Months, 6 Days, 13 Hours, 14 Minutes which would have 4 elements but another might be 2 Months, 1 Week, 3 Days, 4 Hours, 39 Minutes which has 5 elements. If do the preg_replace then I won't know which are the months or weeks or days etc.

So how do I find out if it is the week or days or hours etc? Because I want to convert the values into cycles like 39mins/1440mins would be 0.0270833 days, then I will calculate for the hours days weeks months and years and add all of them together

// 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 ***/
    }

}
kross
  • 475
  • 1
  • 11
  • 31

1 Answers1

0

I've decided to abandon that logic and use PHP's Date Time class instead. Got some help from this post as well. Not sure if it's the best way but it's what I've found.

// 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');
}
Community
  • 1
  • 1
kross
  • 475
  • 1
  • 11
  • 31