PHP convert date format dd/mm/yyyy = yyyy-mm-dd
Dates in the
m/d/y
ord-m-y
formats are disambiguated by looking
at the separator between the various components: if the separator is a
slash (/
), then the Americanm/d/y
is assumed; whereas if the
separator is a dash (-
) or a dot (.
), then the Europeand-m-y
format is assumed. Check more here.
Use the default date function.
$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
EDIT I just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.
$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));
Converting to date in PHP from yyyymmdd format
Use strtotime()
to convert a string containing a date into a Unix timestamp:
<?php
// both lines output 813470400
echo strtotime("19951012"), "\n",
strtotime("12 October 1995");
?>
You can pass the result as the second parameter to date()
to reformat the date yourself:
<?php
// prints 1995 Oct 12
echo date("Y M d", strtotime("19951012"));
?>
Note
strtotime()
will fail with dates before the Unix epoch at the start of 1970.
As an alternative which will work with dates before 1970:
<?php
// Returns the year as an offset since 1900, negative for years before
$parts = strptime("18951012", "%Y%m%d");
$year = $parts['tm_year'] + 1900; // 1895
$day = $parts['tm_mday']; // 12
$month = $parts['tm_mon']; // 10
?>
Convert number to date in PHP and format to YYYYMMDD
Using date()
Simply write:
<?php
echo date('Ymd', 1441065600);
Otherwise, using a DateTime instance:
Short
<?php
echo (new DateTime())->setTimestamp(1441065600)->format('Ymd');
Note: the setTimestamp()
method do not takes a string as parameter! Otherwise, you may want to do so:
Long
<?php
$english = '1441065600';
$timestamp = strtotime($english);
$date = new DateTime($timestamp);
echo $date->format('Ymd'); // 20161214
Descriptions
strtotime() - Parse about any English textual datetime description into a Unix timestamp
DateTime - Representation of date and time
Note: I created a new DateTime
instance from the UNIX timestamp, English textual representation may lead to an error.
Other formats
I recommend you to read the DateTime::format() method documentation along with the date() function documentation to learn more about date formats.
Format date from yyyymmdd to Y-m-d with PHP
Use strtotime() to convert a string containing a date into a Unix timestamp:
<?php
// both lines output 813470400
echo strtotime("19951012"), "\n",
strtotime("12 October 1995");
?>
You can pass the result as the second parameter to date() to reformat the date yourself:
<?php
// prints 1995 Oct 12
echo date("Y-m-d", strtotime("19951012"));
?>
Convert a date format in PHP
Use strtotime()
and date()
:
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
(See the strtotime and date documentation on the PHP site.)
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 and format :-)
Convert date from YYYYMMDD to DD/MM/YYYY format in PHP
You can easily use the DateTime class to do this
$retrieved = '20121226';
$date = DateTime::createFromFormat('Ymd', $retrieved);
echo $date->format('d/m/Y');
http://php.net/manual/en/datetime.format.php
Format date dd/mm/yyyy to yyyy-mm-dd PHP
You might want to use DateTime::createFromFormat:
$show_date = DateTime::createFromFormat('d/m/Y', $dateInput)->format('Y-m-d');
Converting string to Date and DateTime
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.
PHP how to convert dd/mm/yy to yyyy-mm-dd
With an ambigous date like you have it is better to use the DateTime
class as you can set the input format. See the following example from the documentation:
$var = '20/01/21';
$date = DateTime::createFromFormat('d/m/y', $var); // "d/m/y" corresponds to the input format
echo $date->format('Y-m-d'); //outputs 2021-01-20
How to convert a date from yyyy-mm-dd to Month(in words) day year?
Use the PHP date
function to reformat it:
echo date('F j, Y',strtotime($row['date'])); // January 30, 2015, for example.
See date for many more formatting options.
Related Topics
Access an Array Returned by a Function
Using Pdo Prepared Statement and Incrementing a Column Value
HTML Purifier: Removing an Element Conditionally Based on Its Attributes
Phpmailer Send Gmail Smtp Timeout
How to Ignore a Moved-Header with File_Get_Contents in PHP
PHP Session Variables Not Being Maintaned
Regex PHP: Find Everything in Div
PHP Xml Inserting Element After (Or Before) Another Element
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method
How to Convert Week Number and Year into Unix Timestamp
PHP Values of One Array to Key of Another Array
Sqlstate[Hy000] [2003] Can't Connect to MySQL Server on '127.0.0.1' (61) Error Laravel 4.1
Using Seo User Friendly in PHP
PHP Loop Through Associative Arrays