Jump to content

What Date Format is this?


doubledee

Recommended Posts

What Date Format is this...

		echo '<div class="date">Published: ' . date('F j, Y', strtotime($publishedOn)) . '</div>';

 

And where can I find a listing of the different Date and Time Formats that will work with this?

 

(I would like to display my date as "2012-02-26  2:40pm" for what it is worth...)

 

Thanks,

 

 

Debbie

Link to comment
Share on other sites

If it isn't there, you simply need to create the formatting string yourself using the formatting parameters in the manual page for date().

 

No, my point was the link you provided had the codes I was looking for, but the page I posted didn't...

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

OK. As long as you found what you needed. If it gives you trouble, post the problem.

 

This is what I came up with...

		echo		'<p class="commentDate">Posted on: ' . date('Y\-m\-d g\:ia', strtotime($createdOn)) . '</p>';

 

 

Is that how you add Text to the Date/Time?

 

 

Debbie

 

 

Link to comment
Share on other sites

Such as what?

 

Such as what what?!  :confused:

 

 

Debbie

 

 

I wasn't sure what text you were referring to when you ask how to add text to a date format.

 

Oh.  I meant if you needed to things like...

 

Published on: 2012-02-21 3:19pm

 

Your Test is on 3/15/2012 at 10:00am in Room 101.

 

It is now 11:30pm on 3/20/2012

 

 

Debbie

 

 

Link to comment
Share on other sites

Hello Debbie,

 

Maybe I can help. You can look at formatting it the way you have with date() or you can use DateTime as below:

 

<?php
// Nice date..
$dt = new DateTime($dateyouareworkingwith);

// Format!
$dt->format("jS F Y")

// Go.
echo $dt;
?>

 

For example using "jS F Y" as the format with the following timestamp: "2012-02-03 21:31:15" will format to "3rd February 2012".

 

So with that all you need to put into DateTime is the timestamp you're working with and then format it in anyway you want without anything special. Equally with using date() you don't need to add blackslashes all over the place so..

 

<?php
// Current datetime in Year-Month-Day Hour:Minutes:Second format..
echo date("Y-m-d H:i:s");
?>

 

That will echo out something like "2012-02-26 23:39:21" for me at the moment. The only reason the "-"'s are in there is to break up what would be just numbers and make it look semi-friendly.

 

Hope it helps!

Link to comment
Share on other sites

Do I only need backslashes when the character I want to use is also a special character (e.g. Y) ?

 

Yes, you only need to escape a character that is also a format code.  Generally you should keep your date format string as free of extra characters as possible.  So if you want to output something like this:

 

Welcome, the current date and time is: 2/26/2012 7:17

 

Rather than include that whole string in the date call, you just do the date part and concatenate that to the rest:

$now = date('m/d/Y g:i');
echo 'Welcome, the current date and time is: <b>'.$now.'</b>';

 

Link to comment
Share on other sites

Do I only need backslashes when the character I want to use is also a special character (e.g. Y) ?

 

Yes, you only need to escape a character that is also a format code.  Generally you should keep your date format string as free of extra characters as possible.  So if you want to output something like this:

 

Welcome, the current date and time is: 2/26/2012 7:17

 

Rather than include that whole string in the date call, you just do the date part and concatenate that to the rest:

$now = date('m/d/Y g:i');
echo 'Welcome, the current date and time is: <b>'.$now.'</b>';

 

Now there is some sage advice!!

 

Thanks, Kicken!

 

 

Debbie

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.