1

我正在使用我发现可以执行此操作的函数,但我正在尝试使其与 GMT UTC 时间戳一起使用:

编辑:也许我的问题是我如何将用户输入时间“转换”为格林尼治标准时间......

我在做

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

实际上并没有gmdate('Y-m-d H:i:s',$the_user_input_date);将其“转换”为gmt?它只是格式化吗?也许那是我的问题。

以下是我可以提供的时间:

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

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

我试图让它像这样工作:

    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

到目前为止,这是我正在使用的内容:

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

@杰森

我试过你在这里建议的:

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';
      }
    }

它似乎总是让我的时间增加 10 个小时。

任何想法可能会发生什么?

也许错误在于我如何节省目标时间?当有人提交时间时,它会像这样转换和存储

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);
4

2 回答 2

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

于 2011-07-21T02:01:08.077 回答
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.

于 2011-07-21T00:20:24.443 回答