Convert a Date Format in PHP

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

how to convert date format to datetime format using php

Why don't you use the DateTime object in PHP (http://php.net/manual/en/class.datetime.php)?

Here is how i would do it:

$date = DateTime::createFromFormat('d/m/Y', $travelDate);
echo $date->format('Y-m-d H:i:s');

Hope it helps.

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

Convert one date format into another in PHP

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

How to change date format d/m/Y to Y-m-d PHP

Use DateTime objects when you're working with dates and times. You can use DateTime::createFromFormat() to parse the date string and then the DateTime::format() to format it the way you want:

$str = '24/12/2013';
$date = DateTime::createFromFormat('d/m/Y', $str);
echo $date->format('Y-m-d'); // => 2013-12-24

For a list of available formatting options, see the documentation.

PHP Date Convert with slashes

There is most likely a better solution than this, but as quick workaround this works:

$passedDate = "30/3/2020 17:7:23:847";
$explodedDateTime = explode(" ", $passedDate);
$explodedDate = explode("/", $explodedDateTime[0]);
$explodedTime = explode(":", $explodedDateTime[1]);

$formattedDate = date("Y-m-d H:i:s", strtotime($explodedDate[2]."-".$explodedDate[1]."-".$explodedDate[0]." ".$explodedTime[0].":".$explodedTime[1].":".$explodedTime[2]));

Php Convert date time like 2017-01-10T18:00:00.000Z to standard time

you can use date('Y-m-d h:i:s', strtotime($yourDate));


Hope it will help you :)

php convert date format Manual dd/mm/yyyy

Use strtotime().

$sqldate = "2016-03-21";
echo $newDate = date("d/m/Y", strtotime($sqldate));

Output

21/03/2016

Live Demo : Click Here

Date format change in php

Use this code :

$originalDate = "12/May/2018";
$originalDate =str_replace("/", " ", $originalDate);
$newDate = date("Y-m-d", strtotime($originalDate));
echo $newDate;

PHP - Convert date to YYYY-MM-DDTHH:MM:SS

T is a format character so you can't use it directly. Escape it (\T) to get a literal T character:

http://php.net/manual/en/function.date.php

You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash.

$invoice_date = date('Y-m-d\TH:i:s');


Related Topics



Leave a reply



Submit