Php Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Php Date

Post by l9pt5 »

I've the following code, but I want to insert some spaces or some characters between the date and the time (betwwen the letter Y and letter g below), how do I do that? I tried with the space but it doesn't work.

Code: Select all

date("n/d/Y  g:i a", $somedate)
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Are you trying to use more than one space? Does one space work?
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Post by l9pt5 »

Yes, I'm trying to use more than one space.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Either add escaped characters into the format string or create the date component, add your additional text, then the time component. I can only assume you're attempting to see this in a browser, in which case you will need   for additional spaces beyond one.
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Post by l9pt5 »

ok, I tried the escaped characters for the spaces in the code below

Code: Select all

echo date("m/d/Y \s\s\s\s g:i:s a", mktime());
and I get the following ssss instead of spaces.

05/09/2007 ssss 11:39:51 am
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Are you printing the returned string in an HTML document? If so, read feyd's post
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Post by l9pt5 »

I want to use the escaped character in the string instead of create a date component and a time component. Am I not using the escaped character correctly?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

HTML or not?
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Post by l9pt5 »

sorry, HTML document.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

As feyd touched on, your browser will render two or more consecutive spaces as one, and the only way to override that is to replace the additional spaces with the escape sequence:  

If you stick that straight into the date() call, you'll have to escape at least a few of those letters lest date() tries to interpret them. date()'s special characters are listed in the manual entry (date()); all are escaped with a backslash.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You can also wrap the date and its surrounding content in <pre></pre>, depending on your situation.
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Post by l9pt5 »

Phewwwwwww, got it to work with the following code. What threwing me off was the letter 'n', I've to use \\

Thanks for the advice everyone.

Code: Select all

echo date("m/d/Y \&\\n\b\s\p\; \&\\n\b\s\p\; \&\\n\b\s\p\; \&\\n\b\s\p\;  g:i:s a", mktime());
Post Reply