1

I've some data which formatted like: 04.09.1953

I want to convert this format to: 1953-09-04

Is there any way or php function to do this?

1
  • Do you want to print your date differently, or to convert an existing? Commented Jan 10, 2012 at 14:56

4 Answers 4

2

just use strtotime() to get a timestamp and then date() to convert that timestamp to the format you need:

$timestamp = strtotime("04.09.1953");
echo date("Y-m-d", $timestamp);

EDIT:
If you're having some "exotic" format as input, you might need to use explode(), list() and mktime() to build the timestamp on your own:

list($y,$m,$d) = explode(".","04.09.1953");
$timestamp = mktime(0,0,0,$m,$d,$y);
echo date("Y-m-d", $timestamp);
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried strtotime() ? It might work, else you'll need to do manual conversion using substrings or explodes.

http://php.net/strtotime

http://php.net/substring

http://php.net/explode

Comments

0

If all you are interested in is converting from one "string" format to another you can use a regEx:

$DMY = '04.09.1953';

$YMD = preg_replace('/(\d\d).(\d\d).(\d{4,4})/', '$3-$2-$1', $DMY);

Comments

0

http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/reformat_date.html

function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}

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.