By matthy

2010-03-21 17:11:12 8 Comments

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?


@Igor Donin 2012-08-07 14:43:06

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

@Igor Donin 2012-08-07 14:48:08

Reading on, I like Ceiroa fix on it. That's way easier than exploding/imploding the date.

@rjmunro 2014-03-04 11:19:06

The top answer is not incorrect. PHP does the right thing with an ISO format date "YYYY-MM-DD". It cannot misinterpret it as a US date, because it has 4 digits at the start, and it uses - not / as a separator. ISO dates were carefully designed to not be confusable with other formats.

@Kevin 2010-03-21 17:13:31

$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));

@richsage 2010-03-21 17:13:38

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 :-)

@Enrique 2010-04-29 22:20:10

What if I get this date 0000-00-00 from mySQL? it returns a wrong date like 31/12/1969 ...

@ShadowStorm 2012-11-04 15:20:20

@Enrique: If you get 000-00-00 from MySQL, you should test against that FIRST before putting it through the conversion. Simples.

@Sobin Augustine 2013-12-06 10:13:00

what if the date is "1000-01-01", its also not working!

@saad 2013-12-20 10:00:28

it is not working. it is giving me 1969 time

@Samuel Lindblom 2014-01-30 07:12:43

@SobinAugustine et al: strtotime() will convert the date string to a "Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)", a format date() can understand and then convert from. So this will not work with a timestamp before 1970.

@Sobin Augustine 2014-01-30 09:47:47

@SamuelLindblom no, its not like that, it will work before 1970.

@Samuel Lindblom 2014-01-30 13:02:55

@SobinAugustine: You are right. It will however not work before 1901-12-14 so that still applies to why the dates listed above do not work.

@Sobin Augustine 2014-01-30 13:11:50

@SamuelLindblom so again, thats my question,"what if the date is "1000-01-01" " there although i found out !! :)

@Dan Chase 2018-11-11 18:05:15

@SobinAugustine It also doesn't work for "Hello-How-Are-You".. smh

@GeneCode 2019-01-15 07:21:05

What if original date is "2010-04-01" ? This could be both January 4th, or 1st April.

@Gabriel 2010-03-21 18:16:56

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 :)

@Voodoo 2014-11-20 23:38:06

I ended up using this because in our application, people would enter "99" if one of the fields (day, month, year) is unknown. Good approach if you have any "fake" dates in your data.

@ceiroa 2012-07-11 15:06:10

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

@Gtx 2012-12-11 11:26:10

Thank you for the DateTime::createFromFormat() - it is really more useful than strtotime()

@Salman Abbas 2014-03-03 20:01:59

NB: PHP 5.3+ required for the first two methods.

@Nina Morena 2014-04-26 12:26:17

Thank you for the new DateTime option. That worked and was a very clean option.

@Taron Saribekyan 2016-01-06 20:35:07

strtotime() usefull for converting true type format (Y-m-d) to other formats, but DateTime::createFromFormat() is universal. It's like STR_TO_DATE in MySQL

@Aiphee 2016-03-14 12:59:40

Oneliner: (new DateTime($date))->format('Y-m-d')

@Code Cooker 2017-08-07 18:35:14

This one just really worked for me Instead of Bullshit strtotime()

@Juancho Ramone 2015-09-29 21:22:07

Use this function to convert from any format to any format

function reformatDate($date, $from_format = 'd/m/Y', $to_format = 'Y-m-d') {
    $date_aux = date_create_from_format($from_format, $date);
    return date_format($date_aux,$to_format);

@Abdul Basit 2014-04-28 17:46:35

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

@Abdul Basit 2014-04-28 17:48:43

For Mysql the date format is Y-m-d like "2014-04-26"

@Sinan 2010-03-21 17:17:03

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

@Raja Ram T 2014-05-20 06:49:17

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'); 

@Vishnu 2012-08-08 14:09:06

Given below is PHP code to generate tomorrow's date using mktime() and change its format to dd/mm/yyyy format and then print it using echo.

$tomorrow = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
echo date("d", $tomorrow) . "/" . date("m", $tomorrow). "/" . date("Y", $tomorrow);

@Alper Sarı 2011-12-19 13:47:27

$newDate = preg_replace("/(\d+)\D+(\d+)\D+(\d+)/","$3-$2-$1",$originalDate);

This code works for every date format.

You can change the order of replacement variables such $3-$1-$2 due to your old date format.

@Lê Văn Chiến 2018-03-15 08:35:09

Thanks your so much!

@Tim Lytle 2010-03-21 18:23:15

implode('-', array_reverse(explode('-', $date)));

Without date conversion overhead, not sure it'll matter much.

@Tim Lytle 2010-03-21 18:44:17

Yeah, answered as you submitted yours I believe. Of course the error checking found in strtotime may be something useful.

@Chris Jacob 2010-09-01 00:42:21

Was useful for me in converting non-American date format... dd-mm-yyyy => yyyy-mm-dd See

@Naeem Ul Wahhab 2013-03-12 19:42:22

Don't Understand why this answer has less votes. It should be top voted answer. It solves the problem just simply and fast without any issues. Thanks

@Tim Lytle 2013-03-15 01:46:52

@TheNoble-Coder I don't understand it either. :)

@Naeem Ul Wahhab 2013-03-20 05:35:37

@TimLytle May be people are too busy to sign in and up-vote useful answers/questions. I always up-vote a question and answer if it solves my problem. Up-voting is the only way of appreciation here.

@Tim Lytle 2013-03-20 21:23:12

@TheNoble-Coder I'm pretty sure it's because that answer was an hour older. On SO that matters as much as (if not more than) correctness/usefulness.

@Pedro Araujo Jorge 2014-03-04 15:17:43

You can even call it again if you need to reverse the date format to its original form. Best answer indeed. :)

@Matteo Bononi 'peorthyr' 2014-11-22 17:03:52

this should be changed as accepted answer, it resolve all the date-time issue of using date and date formats.

@krishna 2015-06-12 06:51:47

@MatteoBononi'peorthyr' it doesn't solve all date-time issues. What if date format is d-M-Y and want to convert to Y-m-d.

@KingRider 2015-09-02 15:19:02

Use explode $date[0]."/".date('m', strtotime($date[1]))."/".$date[2] is easier.. converter month to number, not work try strtotime is cause time zone.

Related Questions

Sponsored Content

47 Answered Questions

[SOLVED] How to format a JavaScript date

42 Answered Questions

[SOLVED] How do I get the current date in JavaScript?

  • 2009-10-07 11:39:02
  • Suresh
  • 2094521 View
  • 1836 Score
  • 42 Answer
  • Tags:   javascript date

39 Answered Questions

[SOLVED] Detecting an "invalid date" Date instance in JavaScript

  • 2009-08-30 11:34:40
  • orip
  • 619633 View
  • 1174 Score
  • 39 Answer
  • Tags:   javascript date

8 Answered Questions

[SOLVED] Converting a UNIX Timestamp to Formatted Date String

  • 2012-04-06 07:08:25
  • Utku Dalmaz
  • 371330 View
  • 157 Score
  • 8 Answer
  • Tags:   php unix-timestamp

61 Answered Questions

[SOLVED] How can I format numbers as dollars currency string in JavaScript?

35 Answered Questions

[SOLVED] Where can I find documentation on formatting a date in JavaScript?

36 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

12 Answered Questions

[SOLVED] Convert dd-mm-yyyy string to date

  • 2011-08-22 18:01:27
  • user559142
  • 553525 View
  • 152 Score
  • 12 Answer
  • Tags:   javascript date

35 Answered Questions

[SOLVED] Compare two dates with JavaScript

3 Answered Questions

[SOLVED] php convert date format Manual dd/mm/yyyy

  • 2016-07-18 06:25:19
  • Jaydeep Gondaliya
  • 1002 View
  • 1 Score
  • 3 Answer
  • Tags:   php date

Sponsored Content