1

I'm working with a function I found to do this, but I'm trying to make it work with a GMT utc timestamp:

EDIT: Maybe my issue is with how i'm "converting" the user input time to GMT...

I was doing

$the_user_input_date = strtotime('2011-07-20T01:13:00');
$utctime = gmdate('Y-m-d H:i:s',$the_user_input_date);

Does gmdate('Y-m-d H:i:s',$the_user_input_date); not actually "convert" it to gmt? does it just format it? Maybe thats my issue.

Here's what the times I can supply would look like:

//local time in GMT
2011-07-20T01:13:00

//future time in GMT
2011-07-20T19:49:39

I'm trying to get this to work like:

    Started 36 mins ago
    Will start in 33 mins
    Will start in 6 hrs 21 mins
    Will start in 4 days 4 hrs 33 mins

Here's what im working with so far:

EDIT: new php code im working with, seems to ADD 10 HOURS on to my date. Any ideas? I  updated it here:

function ago($from)
 {
  $to = time();

  $to = (($to === null) ? (time()) : ($to));
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
  $from = ((is_int($from)) ? ($from) : (strtotime($from)));

  $units = array
  (
   "year"   => 29030400, // seconds in a year   (12 months)
   "month"  => 2419200,  // seconds in a month  (4 weeks)
   "week"   => 604800,   // seconds in a week   (7 days)
   "day"    => 86400,    // seconds in a day    (24 hours)
   "hour"   => 3600,     // seconds in an hour  (60 minutes)
   "minute" => 60,       // seconds in a minute (60 seconds)
   "second" => 1         // 1 second
  );

  $diff = abs($from - $to);
  $suffix = (($from > $to) ? ("from now") : ("ago"));

  foreach($units as $unit => $mult)
   if($diff >= $mult)
   {
    $and = (($mult != 1) ? ("") : ("and "));
    $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s"));
    $diff -= intval($diff / $mult) * $mult;
   }
  $output .= " ".$suffix;
  $output = substr($output, strlen(", "));

  return $output;
 }

@Jason

I tried what you suggested here:

function ago($dateto)
    {
      $datetime1 = new DateTime( $dateto);
      $datetime2 = new DateTime();
      $interval = $datetime1->diff($datetime2);
      // print_r($interval);

      $format = '';
      if ($interval->h) {
              $format .= ' %h ' . ($interval->h == 1 ? 'hour' : 'hours');
      }
      if ($interval->i) {
              $format .= ' %i ' . ($interval->i == 1 ? 'minute' : 'minutes');
      }
      // more logic for each interval

      if ($format) {
              echo $interval->format($format), ' ago';
      }
      else {
              echo 'now';
      }
    }

It always seems to add 10 hours on to my time.

Any ideas what could be going on?

Maybe an error lies with how I'm saving the target time? When someone submits a time its converted and stored like this

The user submitted time will always start out looking like this as their local time: 07/20/2011 11:00 pm

Then:

$time = mysql_real_escape_string($_POST['time']);

$the_date = strtotime($time);

//make user input time into GMT time
$utctime = gmdate('Y/m/d H:i:s',$the_date);

$query = "INSERT INTO $table (time) VALUES ('$utctime');";
mysql_query($query);
brybam
  • 5,009
  • 12
  • 51
  • 93
  • What version of PHP are you using? A lot of this functionality is built into the `DateTime` classes in PHP >= 5.3. – Jason McCreary Jul 21 '11 at 00:07
  • `PHP 5.2 FastCGI` is what it's currently set to according to my host provider. It gives me an option to select `PHP 5.3 FastCGI` If I make the switch. How might I do what I'm trying to accomplish? – brybam Jul 21 '11 at 00:31

2 Answers2

2

Provided you have access to PHP >= 5.3 I'd recommend DateTime::diff(). The DateInterval returned gives you all the parts you would need for display as well as has its own methods, such as format().

Here's a sample to give you an idea. There are more complete samples in the comments of the PHP documentation links.

<?php
$datetime1 = new DateTime('2011-07-20');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
// print_r($interval);

$format = '';
if ($interval->h) {
        $format .= ' %h ' . ($interval->h == 1 ? 'hour' : 'hours');
}
if ($interval->i) {
        $format .= ' %i ' . ($interval->i == 1 ? 'minute' : 'minutes');
}
// more logic for each interval

if ($format) {
        echo $interval->format($format), ' ago';
}
else {
        echo 'now';
}

It outputs (on my system):

22 hours 10 minutes ago

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • Thanks Jason, I'm still having some weird errors it seems to be a few hours off. I updated my post to show you what I tried. Any ideas? – brybam Jul 21 '11 at 02:41
  • I believe this stems from your storage of the time. There are many places this could occur - PHP, MySQL, etc. My suggestion would be GMT across the board. – Jason McCreary Jul 21 '11 at 02:53
  • That's what im already doing. If you look above I showed you how I was taking the time and converting it to GMT right as users specify times. Is my process for getting the GMT not correct? (its at the bottom of my question i added it a few minutes ago) – brybam Jul 21 '11 at 02:55
  • `$datetime2 = new DateTime();` gets the current time from *your* system. I'd encourage you to debug these times as well as read more from the docs and try to get a little farther on your own. – Jason McCreary Jul 21 '11 at 03:02
  • Well, I tried `time()` and `DateTime(time())`because I know `time()` is a GMT time. But it's expecting a datetime timestamp. So i tried `$datetime2 = new DateTime(date("Y/m/d\TH:i:s",time()));` to just convert the time() to something datetime can accept. But that's still giving me time about 3 hours ago rather than whats expected. On a side note, im usually working with flex builder (eclipse) im not quite sure how to debug php. Eclipse makes debugging a breeze and I feel like im pretty much in the dark when doing php on the browser. I don't do it too often. – brybam Jul 21 '11 at 03:20
  • try running `date_default_timezone_get()` just to make sure your PHP server is using GMT for all date functions. It looks to me like your timezone settings are off somewhere. – Tarek Fadel Jul 21 '11 at 03:30
  • Im not sure what getting the time zone would do because everything should be in GMT. But, I did it just to see, and it says `America/Los_Angeles` Which I still dont think it should matter because im dealing in GMT. You can see above at the bottom of my question how I am converting the user input time to GMT, and im verifying it in my mysql database that it is in GMT – brybam Jul 21 '11 at 03:46
  • Also, I just wanted to say I tried `$datetime2 = new DateTime(gmdate('Y/m/d\TH:i:s',time()));` which is the same converstion method I use on the user input time. Still getting a result that is hours off though. – brybam Jul 21 '11 at 03:48
  • Also tried, `$datetime2 = new DateTime(gmdate('Y/m/d H:i:s'));` because according to the docs its the GMT time...But its still saying like `3 hours ago` for a time that should be `1 hour from now` – brybam Jul 21 '11 at 04:28
0

Your $datefrom is a string, but $dateto is an int. You can't subtract them that way.

Instead of:

$datefrom=gmdate("Y/m/d\TH:i:s\Z");

Do:

$datefrom=time();

PS. I did not check the rest of the code.

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • I tried exactly what you suggested. then tried `echo ago('2011/07/20/T21:23:00Z')` which is an hour from now, and it displayed `-10919 seconds ago` so I don't think that worked :/ – brybam Jul 21 '11 at 00:26
  • `2011/07/20/T21:23:00Z` is an hour from now EDT. Do `2011/07/20/T21:23:00 EDT` and it's correct. Or just `2011/07/20/T21:23:00` for local time. – Ariel Jul 21 '11 at 00:38
  • Another thing, if i do time() doesn't that just get the server time? I need it to be GMT so its always a standardized time. thats why I was trying to use gmdate – brybam Jul 21 '11 at 01:32
  • Sorry about that i was just looking into it. Your right about that. But I just tried it again with `2011/07/20T22:38:00` which is an hour from now but it said 3 hours ago. Any ideas? – brybam Jul 21 '11 at 01:39
  • Is this reversed? `$difference = $dateto - $datefrom;` I think it might need to be `$difference = $datefrom - $dateto;` – Ariel Jul 21 '11 at 02:59
  • It's 11:30pm where I am right now, I just tried it with `2011/07/21T00:00:00` (12am on the 21st)and it displayed `-4 hours ago` when i switched them. So i don't think switching those did it – brybam Jul 21 '11 at 03:31
  • You do need to switch them. Also in `strtotime('-1 min', $dateto) < $datefrom` swap the `$datefrom` and `$dateto`. – Ariel Jul 21 '11 at 04:39
  • I updated my php code to a better snippet I found. I did a test where at 1:05am I submitted an event at 1:30am but it says `10 hours, 25 minutes from now` I have no idea where its getting the extra 10 hours. Everything is in GMT – brybam Jul 21 '11 at 05:09
  • Is it possible im not converting the user input time to GMT properly? Here's what I was doing: `$the_user_input_date = strtotime('2011-07-20T01:13:00'); $utctime = gmdate('Y-m-d H:i:s',$the_user_input_date);` – brybam Jul 21 '11 at 05:24
  • That code seems ok, but of course you have a unixtime, not a string. – Ariel Jul 21 '11 at 06:12