开发者

php "countdown til" and "since time" GMT UTC time function

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开发者_如何学C 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);


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜