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

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

Expect output: '2011-08-14 20:34:12 +1 hour'

I know there is error on the double quotation, but don't know how to fix it. Can anyone help me out? It would be really appreciate anyone can give some explanation, Thanks!

hakre
  • 193,403
  • 52
  • 435
  • 836
Acubi
  • 2,793
  • 11
  • 41
  • 54

7 Answers7

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

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

echo($dateTime);
Whetstone
  • 1,199
  • 8
  • 8
2

Use this:

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

or this:

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

and then

echo($dateTime);
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
  • 1
    Yeah ok but at worst you're teaching a PHP noob a bad/confusing practice. – Dunhamzzz Oct 24 '11 at 16:02
  • @Dunhamzzz Bad pratice is different from wrong answer so you should delete all the downvote (because you think they'll not work). – Aurelio De Rosa Oct 24 '11 at 16:03
  • 1
    @Dunhamzzz: the return value of an assignment operation is the value that was assigned, not "true". `$a = $b = $c = 7` is perfectly valid in PHP, and assigns `7` to all 3 vars. – Marc B Oct 24 '11 at 16:33
  • @MarcB that is not what my original comment was regarding. – Dunhamzzz Oct 24 '11 at 16:38
  • Yes, it was. You were complaining about `echo $dateTime = 'x' . 'y'`, and I'm telling you that this would echo the concatenated string 'xy', not "true", because that's the value that was assigned to dateTime. – Marc B Oct 24 '11 at 16:40
1

You can concatenate the string

echo $dateTime = $_SESSION['result']." +1 hour";
Kurt Funai
  • 644
  • 3
  • 7
1

I suggest you read about strings in the PHP docs. What you want here is called concatenation.

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

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

echo $dateTime;

Also notice the last line you want to echo $dateTime after you have set its contents.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
1

You can find many examples of how to access array elements under 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;
VolkerK
  • 95,432
  • 20
  • 163
  • 226
0

Use the single quote for string and nothing for variable, in your case.

echo $dateTime = $_SESSION['result'].' +1 hour';
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
0

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

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

Try the above.

Community
  • 1
  • 1
Nick
  • 6,316
  • 2
  • 29
  • 47