Jump to content

Recommended Posts

Hi Guys,

 

Was wondering if anyone knew how to go about changing the format of a date? I have a variable which currently prints the date like this:

 

2012-12-13 01:01:00

 

But I need to change it to be displayed like this:

 

 

13th December 2012

 

And also like this:

 

63 Days left

 

 

 

Any ideas how I would do this? Im guessing the "time left" is a little trickier, but would be great if possible?

 

Thanks guys, really hope somone can help. :)

Link to comment
https://forums.phpfreaks.com/topic/269309-convert-date-to-different-format/
Share on other sites

DateTime::createFromFormat() can be used to create a DateTime object from your variable. Then you can use format() to display it in another format, and diff() to get the number of "days left".

 

Let us know how you get on.

^+1

also worth to take a look to the many functions at your disposition in php

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

 

and if your variable is coming from a mysql resultset, mysql also provide the functions to do that directly in your query

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Hi Guys,

 

Thanks for the replies, really appreciated. I'm having a few problems though, I can only seem to find examples that change the y/d/m around, not get the actual word like December in? or with the "th" after 13.

 

Am I missing something?

 

Thanks

 

Mike

You want the word "December"?

 

1) Open up the page

 

2) Ctrl+F

 

3) Type "December"

 

4) Find this row:

F             A full textual representation of a month, such as January or March              January through December

 

5) Use it inside the first argument to date: date('F');

 

6) ???

 

7) Get spoon fed the answer

Edited by ManiacDan

Hmmm, no not really. I more of a "give a man a fishing rod" kinda guy. however....something along the lines of

 

"Yeah sure, you need to wrap your variable around this, and then do this and that. The ** means this and the ** means that. Then that should give you the kind of functionality you require. Here's a quick example to help you, this would each January."

 

...would have been nice!!

 

I appreciate your help, I really do, but if I enjoyed helping people on forums and someone had a question about SEO, I wouldn't just put up a link to Google.

 

Not whining, just need help. :-(

You really honestly spent three hours reading the date() manual page? There's examples and everything. What part was confusing? You were given a fishing rod and a book on fishing and pointed toward the nearest body of water.

 

We didn't point you to google, we pointed you to the document on how to do what you asked. The manual. Read it. Get the t-shirt

You want the word "December"?

 

1) Open up the page

 

2) Ctrl+F

 

3) Type "December"

 

4) Find this row:

F A full textual representation of a month, such as January or March January through December

 

5) Use it inside the first argument to date: date('F');

 

6) ???

 

7) Get spoon fed the answer

 

 

 

Why, just why? Do you like being big and clever? I asked for help, tried for 3 hours, come back and said I'm still having trouble, can you help me any more please? And you get all arsey about it! Remember, im not the expert, hence why im here asking for help. It might look simple to you, and probably will to me after I figure it out, but currently I dont get it.

You really honestly spent three hours reading the date() manual page? There's examples and everything. What part was confusing? You were given a fishing rod and a book on fishing and pointed toward the nearest body of water.

 

We didn't point you to google, we pointed you to the document on how to do what you asked. The manual. Read it. Get the t-shirt

 

Hence why the world invented schools and universities. Not just throw a book at a baby and expert a Physicist in a few years.

Most of us belong to the other forums too.

 

Speaking of the world inventing teachers, I asked you a question: what was confusing? If you want us to do more than show you the page with the exact answer you want, explain why you couldn't find the answer even when given the manual entry.

 

 

DateTime::createFromFormat() can be used to create a DateTime object from your variable. Then you can use format() to display it in another format, and diff() to get the number of "days left".


To give you a basic example, putting the above to good use, lets take your original variable containing 2012-12-13 01:01:00 and do some magic.

 

<?php

$subject = '2012-12-13 01:01:00';

// Parse the subject date string as the given format
$datetime = DateTime::createFromFormat('Y-m-d H:i:s', $subject);

// Note, the above is a normal format that DateTime can handle automatically
// so you could have done: (see http://php.net/datetime.formats and sub-pages)
// $datetime = new DateTime($subject);

// Lets get this DateTime object as another format
// See http://php.net/date for what the format letters mean
$reformatted = $datetime->format('jS F Y');
echo "Subject date is $reformatted. ";

// Lets get the number of days between now and the subject date
// Note: DateTime::diff() returns a DateInterval object, see http://php.net/dateinterval
$now = new DateTime;
$interval = $datetime->diff($now);
$direction = $interval->invert ? "left" : "ago";
printf("That's %d days %s.", $interval->days, $direction);

 

The above will output something like the following.

 

Subject date is 13th December 2012. That's 63 days left.

 

Have a play yourself, with a running example similar to the code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.