yyyymmdd - php date format yyyy-mm-dd hh mm ss

Convert date format yyyy-mm-dd=> dd-mm-yyyy (8)

This question already has an answer here:

I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string.

How is this possible?

Edit: Because this post's answer sometimes gets upvoted, I came back here to kindly ask people not to upvote it anymore. My answer is ancient, not technically correct, and there are several better approaches right here. I'm only keeping it here for historical purposes.

Although the documentation poorly describes the strtotime function, @rjmunro correctly addressed the issue in his comment: it's in ISO format date "YYYY-MM-DD".

Also, even though my Date_Converter function might still work, I'd like to warn that there may be imprecise statements below, so please do disregard them.

The most voted answer is actually incorrect!

PHP strtotime Manual here states that "The function expects to be given a string containing an English date format". What it actually means is that it expects an American US date format, such as "m-d-Y" or "m/d/Y".

That means that a date provided as "Y-m-d" may get misinterpreted by strtotime. You should provide the date in the expected format.

I wrote a little function to return dates in several formats. Use and modify as will. If anyone does turn that into a Class, I'd be glad if that would be shared.

function Date_Converter($date, $locale = "br") {

    # Exception
    if (is_null($date))
        $date = date("m/d/Y H:i:s");

    # Let's go ahead and get a string date in case we've been given a Unix Time Stamp
    if ($locale == "unix")
        $date = date("m/d/Y H:i:s", $date);

    # Separate Date from Time
    $date = explode(" ", $date);

    if ($locale == "br") {
        # Separate d/m/Y from Date
        $date[0] = explode("/", $date[0]);
        # Rearrange Date into m/d/Y
        $date[0] = $date[0][1] . "/" . $date[0][0] . "/" . $date[0][2];

    # Return date in all formats
        # US
        $Return["datetime"]["us"]   = implode(" ", $date);
        $Return["date"]["us"]       = $date[0];
        # Universal
        $Return["time"]         = $date[1];
        $Return["unix_datetime"]    = strtotime($Return["datetime"]["us"]);
        $Return["unix_date"]        = strtotime($Return["date"]["us"]);
        $Return["getdate"]          = getdate($Return["unix_datetime"]);
        # BR
        $Return["datetime"]["br"]   = date("d/m/Y H:i:s", $Return["unix_datetime"]);
        $Return["date"]["br"]       = date("d/m/Y", $Return["unix_date"]);

    # Return
    return $Return;

} # End Function

Also another obscure possibility:

$oldDate = '2010-03-20'
$arr = explode('-', $oldDate);
$newDate = $arr[2].'-'.$arr[1].'-'.$arr[0];

I don't know if I would use it but still :)

If you'd like to avoid strtotime conversion(for example strtotime is not being able to parse your input) you can use,

$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString);
$newDateString = $myDateTime->format('d-m-Y');

or, equivalently:

$newDateString = date_format(date_create_from_format('Y-m-d', $dateString), 'd-m-Y');

You are first giving it the format $dateString is in. Then you are telling it the format you want $newDateString to be in.

Or if the source-format always is "Y-m-d" (yyyy-mm-dd), then just use DateTime:

$source = '2012-07-31';
$date = new DateTime($source);
echo $date->format('d.m.Y'); // 31.07.2012
echo $date->format('d-m-Y'); // 31-07-2012

Use strtotime() and date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(see strtotime and date docs on the PHP site).

Edit: Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse & format :-)

You can try strftime function. Something like strftime($time, '%d %m %Y');

two ways to implement this


$date = strtotime(date); 
$new_date = date('d-m-Y', $date);


$cls_date = new DateTime($date);
echo $cls_date->format('d-m-Y'); 

$timestamp = strtotime(your date variable); 
$new_date = date('d-m-Y', $timestamp);

For more:

click here

Or even shorter:

$new_date = date('d-m-Y', strtotime(your date variable));

date('m/d/Y h:i:s a',strtotime($val['EventDateTime']));