If I have a PHP string in the format of mm-dd-YYYY
(for example, 10-16-2003), how do I properly convert that to a Date
and then a DateTime
in the format of YYYY-mm-dd
? The only reason I ask for both Date
and DateTime
is because I need one in one spot, and the other in a different spot.

- 2,961
- 1
- 16
- 24

- 4,493
- 3
- 22
- 16
-
Best will be using DateTime in php 5 & 7. Detail blog: http://sforsuresh.in/converting-string-to-date-and-datetime-PHP – Suresh Kamrushi Nov 20 '20 at 06:35
-
how convert to 10th January 2020 to timeamp? – Amit Bera Jan 15 '22 at 15:31
13 Answers
Use strtotime()
on your first date then date('Y-m-d')
to convert it back:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Make note that there is a difference between using forward slash /
and hyphen -
in the strtotime()
function. To quote from php.net:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

- 42,752
- 13
- 76
- 103
-
12
-
-
9
-
-
Yes I solved this problem with " str_replace('/', '-', $var);" after apply "date('Y-m-d'....' and works well – Apr 06 '16 at 09:34
-
8It doesn't work because this answer is misleading. `strtotime` accepts certain formats. You can't feed it whatever. That's why `DateTime::createFromFormat` should be used instead of `strtotime`. Sadly, this answer has too many upvotes for anyone to pay attention. It's all copy paste these days. – N.B. Apr 03 '18 at 20:54
-
One function should be added like this for better output: $newformat = new DateTime(date('Y-m-d',$time) ); – JWC May Dec 17 '19 at 19:33
-
I'm not sure why PHP assumes d-m-Y while ISO 8601 was created in 1988 which states date should be in format YYYY-MM-DD – AaA Feb 04 '20 at 01:48
-
Please correct your answer, at some point sentence `If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.` added to the quote you have here. meaning if 4 digit year included as first token, it will be `yyyy-mm-dd` and if it is the last then `dd-mm-yyyy` is assumed. a `xx-xx-xx` format will always be considered as `yy-mm-dd` – AaA Mar 03 '20 at 01:45
-
Bit offtopic but worth noting for others because easily forgotten (IMO): be cautios with dateformats like dd.mm.yy because it could be parsed as time. For example date('Y-m-d H:i:s', strtotime('05.03.21')) results in todays date with 05:03:21 for the time. – aProgger Oct 21 '21 at 08:54
-
Or Simply use following code $time = ; `$newformat = date('Y-m-d',strtotime('10/16/2003'));` – w.Daya Nov 27 '21 at 05:34
-
I want to know how to get the year of a date from the database. I passed it as a parameter in the function and did something like: $dt = ($detData == 'yyyy') ? " auto_dtinfr " : " NULL "; I need to implement a to_char. How to do? – André Dec 19 '22 at 15:02
You need to be careful with m/d/Y and m-d-Y formats. PHP considers /
to mean m/d/Y and -
to mean d-m-Y. I would explicitly describe the input format in this case:
$ymd = DateTime::createFromFormat('m-d-Y', '10-16-2003')->format('Y-m-d');
That way you are not at the whims of a certain interpretation.

- 47,584
- 11
- 86
- 98
-
47Curious as to why this isn't the accepted answer... This is much more flexible than relying on the quirks of the `strtotime()` function. – jzimmerman2011 Dec 04 '12 at 21:43
-
10It's important to point that php **DateTime** class is available since PHP version 5.2 and _createFromFormat_ since 5.3 – Diego Shevek Sep 23 '13 at 19:10
-
16As with every function, you should check if it meets minimum version you must support. But PHP 5.2 EOL was Jan 2011. Nobody should be running it any more; catering to those people is just enabling them to run outdated, insecure software. – Matthew Sep 24 '13 at 02:24
-
@Matthew yes, correct. But you never now, even today I found a PHP4.1 version still installed ... – Roland Nov 26 '18 at 16:45
-
-
1I agree with commenters saying **this** should be the **accepted answer,** unless for some godforsaken reason you happen to be stuck with PHP 4 point something. In that case, date formatting ought to be the least of your worries. – UncaAlby Apr 25 '23 at 17:48
To parse the date, you should use: DateTime::createFromFormat();
Ex:
$dateDE = "16/10/2013";
$dateUS = \DateTime::createFromFormat("d.m.Y", $dateDE)->format("m/d/Y");
However, careful, because this will crash with:
PHP Fatal error: Call to a member function format() on a non-object
You actually need to check that the formatting went fine, first:
$dateDE = "16/10/2013";
$dateObj = \DateTime::createFromFormat("d.m.Y", $dateDE);
if (!$dateObj)
{
throw new \UnexpectedValueException("Could not parse the date: $date");
}
$dateUS = $dateObj->format("m/d/Y");
Now instead of crashing, you will get an exception, which you can catch, propagate, etc.
$dateDE has the wrong format, it should be "16.10.2013";

- 1,694
- 13
- 12
-
Notice that checking $dateObj that way would only asure that the format matches the mask, not that the date is actually valid. For example, a date like 99/99/2010 would pass this check since it matches m/d/Y but it's not a date that you usually want to allow. This answer addresses this situation -> https://stackoverflow.com/a/10120725/995014 – Kilian Perdomo Curbelo Jan 23 '18 at 12:19
$d = new DateTime('10-16-2003');
$timestamp = $d->getTimestamp(); // Unix timestamp
$formatted_date = $d->format('Y-m-d'); // 2003-10-16
Edit: you can also pass a DateTimeZone to DateTime() constructor to ensure the creation of the date for the desired time zone, not the server default one.

- 13,688
- 3
- 45
- 55
-
6
-
It's important to point that php **DateTime** class is available since PHP version 5.2 and _getTimestamp_ since 5.3 – Diego Shevek Sep 23 '13 at 19:08
Since no one mentioned this, here's another way:
$date = date_create_from_format("m-d-Y", "10-16-2003")->format("Y-m-d");

- 846
- 11
- 18
To create a date from any string you can use:
$date = DateTime::createFromFormat('d-m-y H:i', '01-01-01 01:00');
echo $date->format('Y-m-d H:i');

- 1,176
- 2
- 15
- 35

- 141
- 1
- 2
If you have the date formatted as "07/May/2018" and need to make it into "2018-05-07" so that it is MySQL compatible, you can use:
if (!empty($date)) {
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
$timestamp = strtotime(str_replace('/', '-', $date));
}
$date = date('Y-m-d', $timestamp);
}

- 3,008
- 4
- 26
- 44

- 332
- 3
- 9
For first Date
$_firstDate = date("m-d-Y", strtotime($_yourDateString));
For New Date
$_newDate = date("Y-m-d",strtotime($_yourDateString));

- 958
- 10
- 12
If you have format dd-mm-yyyy then in PHP it won't work as expected. In PHP document they have below guideline.
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
So, you just can't use as you wish. When your try to use dd/mm/yyyy format with this then it will remove FALSE. You can tweak with the following.
$date = "23/02/2013";
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
$timestamp = strtotime(str_replace('/', '-', $date));
}
echo $timestamp; // prints 1361577600

- 592
- 6
- 10
I try all the above answers, but fail for me where in my case I built a function that get date string with the following format 'YYYY/mm/dd' . so i think much better to explode the string like that:
$old_date = explode('/', $coming_date_str);
$new_data = $old_date[0].'-'.$old_date[1].'-'.$old_date[2];
// this to convert the string as all the above posts and complete
$new_date = date('Y-m-d', strtotime($new_data));
$new_date = DateTime::createFromFormat("Y-m-d", new_date);

- 37
- 1
- 4
It worked for me to do something like:
$stringDate = "2022-02-24T17:15:00"; (if you only send the date "2022-02-24" it fills the time in 00:00:00).
$dateFormat = new DateTime($miStringDate);
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '22 at 20:33
If you wish to accept dates using American ordering (month, date, year) for European style formats (using dash or period as day, month, year) while still accepting other formats, you can extend the DateTime class:
/**
* Quietly convert European format to American format
*
* Accepts m-d-Y, m-d-y, m.d.Y, m.d.y, Y-m-d, Y.m.d
* as well as all other built-in formats
*
*/
class CustomDateTime extends DateTime
{
public function __construct(string $time="now", DateTimeZone $timezone = null)
{
// convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y (substr avoids microtime error)
$time = str_replace(['-','.'], '/', substr($time, 0, 10)) . substr($time, 10 );
parent::__construct($time, $timezone);
}
}
// usage:
$date = new CustomDateTime('7-24-2019');
print $date->format('Y-m-d');
// => '2019-07-24'
Or, you can make a function to accept m-d-Y and output Y-m-d:
/**
* Accept dates in various m, d, y formats and return as Y-m-d
*
* Changes PHP's default behaviour for dates with dashes or dots.
* Accepts:
* m-d-y, m-d-Y, Y-m-d,
* m.d.y, m.d.Y, Y.m.d,
* m/d/y, m/d/Y, Y/m/d,
* ... and all other formats natively supported
*
* Unsupported formats or invalid dates will generate an Exception
*
* @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
* @param string $d various representations of date
* @return string Y-m-d or '----' for null or blank
*/
function asYmd($d) {
if(is_null($d) || $d=='') { return '----'; }
// convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
$d = str_replace(['-','.'], '/', $d);
return (new DateTime($d))->format('Y-m-d');
}
// usage:
<?= asYmd('7-24-2019') ?>
// or
<?php echo asYmd('7-24-2019'); ?>

- 2,614
- 1
- 15
- 23
If you want to get the last day of the current month you can do it with the following code.
$last_day_this_month = date('F jS Y', strtotime(date('F t Y')));

- 2,161
- 25
- 39