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));
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
convert dd-mm-yyyy to yyyy-mm-dd in php
Try with split like
$a = split('-',$_POST["txtstartdates"]);
or you can use explode even like
$a = explode('-',$_POST["txtstartdates"]);
$my_new_date = $a[2].'-'.$a[1].'-'.$a[0];
Here strtotime will not work for the format dd-mm-yyyy
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');
How to convert date format from dd/mm/yyyy to yyyy-mm-dd using carbon on Laravel
You can try this:
Carbon::createFromFormat('d/m/Y', $request->stockupdate)->format('Y-m-d')
How to change date format from DD/MM/YYYY to YYYY-MM-DD?
$date = "06/16/2010";
echo date('Y-m-d', strtotime($date)); // outputs 2010-06-16
Using the strtotime function.
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 to date format dd/mm/yyyy
You can use a regular expression or some manual string fiddling, but I think I prefer:
date("d/m/Y", strtotime($str));
How to convert date from yyyy-mm-dd to dd-mm-yyyy in php?
use this function, which uses inbuilt function 'substr(str,start,length)'.
function getFormatDate($date){
$date = substr($date,8,2).'-'.substr($date,5,2).'-'.substr($date,0,4);
return $date;
}
getFormatDate($data2['emp_dob']);
Related Topics
PHP Curl Not Working - Wamp on Windows 7 64 Bit
When to Use Strtr VS Str_Replace
How to Execute PHP That Is Stored in a MySQL Database
Can't Access Global Variable Inside Function
Why Is This PHP Call to Json_Encode Silently Failing - Inability to Handle Single Quotes
How to Check If a String Starts With a Specified String
PHP: Check If an Array Contains All Array Values from Another Array
PHP X86 How to Get Filesize of ≫ 2 Gb File Without External Program
Send Post Request Using Volley and Receive in PHP
How to Override the Path of PHP to Use the Mamp Path
Difference Between Method Calls $Model-≫Relation(); and $Model-≫Relation;
Mongodb Duplicate Documents Even After Adding Unique Key
Redirecting to Previous Page After Login - PHP
Convert PHP Date to MySQL Format