0

I'm using a jQuery datepicker where obviously the date format doesn't match php format.

How could I convert the datepicker format over to php format so that I can output date( $format ) in the same convention defined in the datepicker format. So say I put the format like dd/mm/yyyy, how could I convert that to m/d/o in php? Or if I specified it as d-m-yy, how to convert that to n-j-y?

Basically, wondering how I could convert the allowed date picker formats, regardless of what the letters or the separators are to php.

I've tried using preg_replace, but the issue there is with single and double same letters. dd becomes nn, since it's trying to replace d twice. Could something with regex be used maybe? I'm kind of lost on how the logic to convert would be done.

Edit: My Apologies for a oversight in the details. The js format is literally like dd/mm/yyyy, not the actual date output. What it is, in the CMS, the user defines the format the datepicker uses (dd/mm/yyyy or any other format sequence and separators the datepicker has). The PHP is used to output the current date using that defined format and sequence. I would love to have them define a PHP format and then convert that to the js format. But the powers that be always want things the way they want them - sigh).

Edit#2 Here's a working script up on Codepad, which shows the issue with letters in the format being treated individually and doubled up. mm becomes nn instead of just being m. The rest of the characters are being individualized as well.

http://codepad.org/lBgqVgbs

3
  • 1
    I just normally user strtotime() to convert the output from Jquery's datepicker to a timestamp then you can use the date() to convert it into whatever format you want. Commented Jun 13, 2013 at 20:56
  • I failed to mention that the js date format is literally like dd/mm/yyyy, not the actual date output itself. Apologies for that oversight. Commented Jun 13, 2013 at 21:11
  • You might want to check this post too. stackoverflow.com/questions/16702398/… Commented Jan 20, 2014 at 10:27

3 Answers 3

1

this is how would approach it:

$date = '13/06/2013';
$date = implode("-",array_reverse(explode("/",$date)));
$time = strtotime($date);

echo date("m-d-Y H:i:s",$time); // or however you want to format
  1. explode the date by /
  2. reverse the order of the array for proper strtotime() format
  3. implode the pieces for our strtotime() string
Sign up to request clarification or add additional context in comments.

1 Comment

Made an edit to the details - my apologies, but I failed to state the details a bit more clearly.
1

You'll probably want to use DateTime::createFromFormat, which let's you define how a string is formatted. Then, use the format() function of that object to output the date in the way that you want it.

For example:

date_default_timezone_set('Europe/Berlin'); // or any other timezone - you must set a timezone though
$date = DateTime::createFromFormat('d/m/Y', '20/05/1996');
$formatted_date = $date->format('Y-m-d');
var_dump($formatted_date); // output: string(10) "1996-05-20"

You can use almost all of the same formatting letters as for the regular date() function, but if you need extra ones - for instance 'o', you can use the formatted date for that: $yet_another_date = date('m/d/o', strtotime($formatted_date));

1 Comment

It's important to note that if you go with this solution of DateTime, it is mandatory you set the default timezone (ie date_default_timezone_set())
1

Was actually easier than I thought it would be. I didn't know you could use #var#i. So mixing that into the two arrays and using preg_replace...

$pattern = array( '#mm#i', '#m#i', '#dd#i', '#d#i', '#yyyy#i', '#yy#i' );
$replacement = array( 'm', 'n', 'd', 'j', 'o', 'y' );

$date_format = preg_replace( $pattern, $replacement, $option_format );

Many thanks to those that provided answers to my bumbled question. My apologies.

1 Comment

I'd use the PHP format 'Y' instead of 'o' for the year. With the 'o' format, "if the ISO week number (W) belongs to the previous or next year, that year is used instead." (taken from PHP documentation)

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.