1

I'm failing a bit to get this working.

I read out a date from my database in the yyyy-mm-dd format. However, I need to use it in my jQuery ajax call in the dd-mm-yyyy format. Is there a way to turn my dates around? I should've seen this coming when I started working on my app, but alas, I didn't :(

Don't feel like changing around the way I save stuff to my DB so I was wondering if anyone knows an easy way to change the format? Thanks :(

EDIT: Just ran into another, similar problem

I read time out as, for example, 08:00:00 I want to split this into parts aswell. For example

08:00:00 => var a = 8, var b = 00 // ignore seconds

09:30:00 => var a = 9, var b = 30

23:45:00 => var a = 23, var b = 45

10:30:00 => var a = 10, var b = 30

:( Thanks!

1
  • Have you tried strtotime() and then date() to format it back? Edit: see bazmegakapa's answer Commented May 12, 2011 at 14:40

6 Answers 6

3

Format your date directly in your sql query :

SELECT DATE_FORMAT(fielddate,'%d-%m-%Y') as jsDate, DATE_FORMAT(fielddate,'%m-%d-%Y') as phpdate FROM xxx

You can do multiple format in the same query to fit your need (a format for js , one for php, one for direct display ...)

See date and time function

Sign up to request clarification or add additional context in comments.

Comments

2

In PHP you can easily turn it around.

$date=date('d-m-Y', strtotime('2009-11-12'));

You could also achieve this using Javascript:

var date='2009-11-12'.split('-').reverse().join('-');

jsFiddle Demo

EDIT concerning your update:

var timeArray=myTime.split(':'); is what you need here. It grabs a string, and returns a normal array with the elements of the string splitted by :. So timeArray[0] will be the hour, timeArray[1] the minute.

1 Comment

Thanks, this did exactly what I wanted.
2

Yes, you can turn it around, but yyyy-mm-dd is the internationally accepted way to represent dates in computers, so you really should not.

Instead, you should change your database, and if you want to present the date to the user in another format, you do the conversion for the presentation only.

EDIT: Sorry if this answer sounds rude, but I really believe that you will thank me later if you do this. Or at least keep it in mind until next time :)

4 Comments

Actually, yyyy-mm-dd is an accepted method to present dates in HUMAN READABLE format. It's inefficient to store it that way inside memory. Most date/time systems in computers deal in seconds and only convert to the readable format when it comes time to present the date/time as output.
I store my dates in the db in yyyy-mm-dd, which you say is the internationally accepted way, so I don't see how I'm going wrong? I only need to change the date for presentation because I want to use it to work together with jQuery UI. Your answer isn't rude, don't worry, it's these kind of answers that make me think and learn something, so I'm glad you posted. Hope I understood your answer correctly ;) Thanks!
"Actually, yyyy-mm-dd is an accepted method to present dates in HUMAN READABLE format" - Ah, yes, true. But it is often useful to have a middle ground, easily readable for both humans and machines, and as far as I know, yyyy-mm-dd is the de facto standard for this around the world.
"I want to use it to work together with jQuery UI". Ah, since you said jQuery ajax, I supposed that you were going to send the date to a server in dd-mm-yyyy format.
1

use date function:

date("d-m-Y",strtotime($yourdate));

Comments

1
<?php 

$newdate = date('d-m-Y',strtotime($db_date))


?>

Comments

1

Try this: jquery-dateFormat Plugin for jQuery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.