0
$_SESSION['result'] = '2011-08-14 20:34:12';

echo $dateTime = "$_SESSION['result'] +1 hour";

预期输出:'2011-08-14 20:34:12 +1 小时'

我知道双引号有错误,但不知道如何解决。谁能帮我吗?真的很感谢有人能给出一些解释,谢谢!

4

7 回答 7

4
$_SESSION['result'] = '2011-08-14 20:34:12';

$dateTime = "{$_SESSION['result']} +1 hour";

echo($dateTime);
于 2011-10-24T15:52:29.183 回答
2

用这个:

$dateTime = "{$_SESSION['result']} +1 hour";

或这个:

$dateTime = $_SESSION['result'] . " +1 hour";

接着

echo($dateTime);
于 2011-10-24T15:53:22.737 回答
1

您可以连接字符串

echo $dateTime = $_SESSION['result']." +1 hour";
于 2011-10-24T15:52:28.960 回答
1

我建议您阅读PHP 文档中的字符串。您在这里想要的称为串联。

$_SESSION['result'] = '2011-08-14 20:34:12';

$dateTime = $_SESSION['result'] . ' +1 hour';

echo $dateTime;

$dateTime还要注意设置内容后要回显的最后一行。

于 2011-10-24T15:53:36.827 回答
1

您可以在 PHP 中找到许多如何访问数组元素的示例:Array - Array do's and don'ts

$arr = array('foo'=>1234, 'bar'=>array('baz'=>'abcdef'));

// simply no quotes within double-quoted string literals
echo "foo: $arr[foo]\n";
// echo "foo: $arr[bar][baz]\n"; <- doesn't work as intended

// curly-braces -> same syntax as outside of a string literal
echo "foo: {$arr['foo']}\n";
echo "foo: {$arr['bar']['baz']}\n";

// string concatenation
echo "foo: ". $arr['foo'] ."\n";
echo "foo: ". $arr['bar']['baz'] ."\n";

// printf with placeholder in format string
printf("foo: %s\n", $arr['foo']);
printf("foo: %s\n", $arr['bar']['baz']);

// same as printf but it returns the string instead of printing it
$x = sprintf("foo: %s\n", $arr['foo']);
echo $x;
$x = sprintf("foo: %s\n", $arr['bar']['baz']);
echo $x;
于 2011-10-24T16:05:33.827 回答
0

$_SESSION['结果'] = '2011-08-14 20:34:12';

回声 $dateTime = $_SESSION['result'] 。“+1 小时”;

试试上面的。

于 2011-10-24T15:56:07.083 回答
0

在您的情况下,对字符串使用单引号,对变量使用任何内容。

echo $dateTime = $_SESSION['result'].' +1 hour';
于 2011-10-24T15:53:23.567 回答