There seems to be a missing part?


#1

Hi all, have been trying to get this working properly, but can't seem to figure it out, I was fairly sure I was escaping, unescaping the characters where they should be, but it's still not coming out right. The code is below:

<?php date_default_timezone_set('GMT');
echo date("l, \\t\h\e\ jS \of F, Y", strtotime("+1 years")); ?>

It should be appearing, as:

Saturday, the 19th of January, 2019

But, instead, it's appearing as:

Saturday, the 19th of January, 2019

??????????????


#2

Not sure why, but the above last paragraph is appearing above okay, but in reality, it's coming out with 'the' appearing as 'th-'


#4

I'm not sure about the two enclosing backslashes around "the"
\\t\h\e\

http://php.net/manual/en/function.date.php

And I may not be seeing something obvious, but the "should be" and "instead" look identical to me.


#5

Yep, the way the forums was interpreting it was wrong, but I did amend it.


#6

Thanks for the input, I just tried that, and got:
he 19th of January, 2019

(Missing the t)


#8

So it would like this:
\t\h\e jS


#10

Well, the \t is easy. That's "tab" in viewsource that would not be rendered as such.

As for the \e I'm not seeing that happen when it's inside a code fence (i.e.backticks). Are you confusing a quote with code block?


#11

Okay, this forum is messing with the code, could you please put the code below in one of your pages and see what results you get. Many thanks. Dez.

<?php date_default_timezone_set('GMT'); echo date("l, \t\h\\e jS \of F, Y", strtotime("+1 years")); ?>


#13

Thank you - so, like this?

<?php date_default_timezone_set('GMT'); echo date("l, \\t\h\e jS \of F, Y", strtotime("+1 years")); ?>


#14

I still don't get what you mean by "forum messing with the code", but I do see your problem. It has to do with how PHP works with strings. Variables inside double quotes are parsed, single quotes are string literals. eg

<?php
date_default_timezone_set('GMT');
echo date("l, \t\h\\e jS \of F, Y", strtotime("+1 years"));
echo '<hr>';
echo "\r\n";
echo date("l, \t\h\e jS \of F, Y", strtotime("+1 years"));
echo '<hr>';
echo "\r\n";
echo date('l, \t\h\\e jS \of F, Y', strtotime("+1 years"));
echo '<hr>';
echo "\r\n";
echo date('l, \t\h\e jS \of F, Y', strtotime("+1 years"));
?>

view-source

Saturday, 	he 19th of January, 2019<hr>
Saturday, 	he 19th of January, 2019<hr>
Saturday, the 19th of January, 2019<hr>
Saturday, the 19th of January, 2019

rendered output

I expected the double escaped e to have a backslash in view source which I'm not seeing and don't know why not. But anyway, put the format inside single quote marks not double.


#15

Sorry Mittineague, which part needs changing?

<?php date_default_timezone_set('GMT'); echo date("l, \\t\h\e jS \of F, Y", strtotime("+1 years")); ?>


#16

As seen in my example code, the format string.

string date ( string $format [, int $timestamp = time() ] )


#17

You could also do this. If you had to add many words all the escaping would get really sloppy.

$a = explode(" ", date("l, jS F, Y", strtotime("+1 years")));
echo $a[0] . ' the ' . $a[1] . ' of ' . $a[3] . ' ' . $a[4];

EDIT:

put the format inside single quote marks not double.
echo date('l, \t\h\e jS \of F, Y', strtotime("+1 years"));

Better answer to OP's original post.


#18

I think I figured out the escaped e, it looks like the date function is passed through PCRE where \e is "escape"
http://php.net/manual/en/regexp.reference.escape.php

\e
escape (hex 1B)

EDIT
That looks to be the case

echo bin2hex("\t\h\\e");
echo '<hr>';
echo "\r\n";
echo bin2hex("\t\h\e");
echo '<hr>';
echo "\r\n";
echo bin2hex('\t\h\\e');
echo '<hr>';
echo "\r\n";
echo bin2hex('\t\h\e');

(spaces added for readability)

09 5c68 5c65
09 5c68 1b
5c74 5c68 5c65
5c74 5c68 5c65
09 - horizontal tab 
5c - backslash
68 - h
65 - e
1b - escape
74 - t

#19

Thank you all very much, the input above finally cracked it for me, but thank you all - you're all gems!


closed #20

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.