PHP Convert Date Format Dd/Mm/Yyyy =≫ Yyyy-Mm-Dd

PHP convert date format dd/mm/yyyy = yyyy-mm-dd


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. 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



Leave a reply



Submit