1

Ok simple enough problem I hope but I'm probably overthinking the solution.

I have a mysql table with a timestamp formatted datetime. This is how it appears in the database (2016-10-08 03:56:26).

I created a function that is suppose to show the difference between the timestamp and the current time (UTC), which is constantly increasing. Please review my code. Currently it works... somewhat. The issue is when a time stamp is created at Noon (CST). The output will say 11hrs have ellapsed which is not true. All help is appreciated.

<?php
// Set timezone
date_default_timezone_set("America/Chicago");

// Time format is UNIX timestamp or
// PHP strtotime compatible strings
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) {
  // Create temp time from time1 and interval
  $ttime = strtotime('+1 ' . $interval, $time1);
  // Set initial values
  $add = 1;
  $looped = 0;
  // Loop until temp time is smaller than time2
  while ($time2 >= $ttime) {
    // Create new temp time from time1 and interval
    $add++;
    $ttime = strtotime("+" . $add . " " . $interval, $time1);
    $looped++;
  }

  $time1 = strtotime("+" . $looped . " " . $interval, $time1);
  $diffs[$interval] = $looped;
}

$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);
}

?>
cpt-crunchy
  • 391
  • 4
  • 23

3 Answers3

0

Use the PHP object oriented date commands to convert a time from one time zone to another before you do your comparison.

http://php.net/manual/en/datetime.settimezone.php

colonelclick
  • 2,165
  • 2
  • 24
  • 33
  • I think referencing the manual helped. I'll check back with you tomorrow. Thanks. – cpt-crunchy Oct 10 '16 at 01:54
  • Ok. I know this is still not working. I've edited the date_default_timezone_set to ("America/Chicago") and I have one item with a timestamp of **2016-10-09 12:29:05** and the *difference* that is being output is **2 hours 32 minutes 40 seconds**. Please help. Anyone. – cpt-crunchy Oct 10 '16 at 02:59
0

Another approach in PHP is to use Carbon. It has handy methods as well as comparison methods. It's used in Laravel and has a stable position on the market.

Check this: http://carbon.nesbot.com/docs/#api-difference and maybe you'll find out that there's no point of reinventing the wheel.

Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92
0

This worked the best for me. I'll add the link for the question that helped me out with this. LINK HERE

<?php
    $dt        = new DateTime($row['ticketopen']);
    $now       = new DateTime('now');
    $interval  = $dt->diff($now);
?>
Community
  • 1
  • 1
cpt-crunchy
  • 391
  • 4
  • 23