【发布时间】:2011-10-09 20:11:52
【问题描述】:
我正在使用我发现可以执行此操作的函数,但我正在尝试使其与 GMT UTC 时间戳一起使用:
编辑: 也许我的问题在于我如何将用户输入时间“转换”为 GMT...
我在做
$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 个小时。
有什么想法吗?
我如何节省目标时间可能是一个错误? 当有人提交时间时,它会像这样转换和存储
用户提交的时间总是以他们的本地时间开始: 2011 年 7 月 20 日晚上 11:00
然后:
$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);
【问题讨论】:
-
您使用的是什么版本的 PHP? PHP >= 5.3 的
DateTime类中内置了很多这种功能。 -
PHP 5.2 FastCGI是根据我的主机提供商当前设置的。如果我进行切换,它让我可以选择PHP 5.3 FastCGI。我该如何做我想要完成的事情?
标签: php datetime timestamp countdown gmt