mikemeadeuk Posted October 10, 2012 Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
salathe Posted October 10, 2012 Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
mikosiko Posted October 10, 2012 Share Posted October 10, 2012 ^+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 Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 (edited) Did you read format? It links to date Edited October 10, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 You're missing the enormous purple table that's 3 pages long in the link you were provided. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 Hi, ok thanks. Yes I see the purple table, but just spent the last 3 hours trying to work it out. Guess I'll have to go to another forum to get help. Rather than just being redirected to a link. :-/ Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 Can we get a little more whine or rage? That's such a passive aggressive version of "do it for me or I'm leaving". Quote Link to comment Share on other sites More sharing options...
mikosiko Posted October 10, 2012 Share Posted October 10, 2012 .....Guess I'll have to go to another forum to get help. many here will not call that "help", but spoon feeding ... good luck Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 (edited) 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 October 10, 2012 by ManiacDan Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 (edited) 8 ) ??? 9 ) Profit!!! Edited October 10, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 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. :-( Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 That is a very detailed and exact example of what spoon-feeding someone the answer would look like. Thanks. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 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 Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 Anyway, im off to another forum, see if anyone else can help me. thanks for your input anyway. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 I't not going to keep passing arguments back and forth. #movedon Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 NEEDS MORE RAGE!!! Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 $a = '2012-12-13 01:01:00'; echo date('jS F Y', strtotime($a)); See you on the other sites. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 10, 2012 Share Posted October 10, 2012 RAGE!!! (Happy now, Jessica? ) Quote Link to comment Share on other sites More sharing options...
salathe Posted October 10, 2012 Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted October 10, 2012 Author Share Posted October 10, 2012 Hi Salathe, Excellent, thanks a lot for taking the time to help me out! Appreciate it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.