PHP date_format (): how to format a date from a string value

I have some PHP code:

$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;

Used to format the date. The expected result will be 2015-12-01, but it returns 2016-12-01. What am I missing?

+4
source share
3 answers

First use the method createFromFormat, specify the input format:

$exd = DateTime::createFromFormat('d M, Y', '01 Dec, 2015');
// arguments (<format of the input>, <the input itself>)
$exd = date_format($exd, 'Y-m-d'); // then choose whatever format you like
echo $exd;
+8
source

The function date_create()accepts only the link parameter . This function is also a function of the alias.DateTime::__construct()

check date_create_from_format()its function also the function of the alias DateTime :: createFromFormat (). Refer link

$exd = date_create_from_format('j M, Y', '01 Dec, 2015');
//$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
+4
source

date_format (datae_variable, date_format);

<?php
     $exd = date_create('01 Dec, 2015');
     $exd1 = date_format($exd,"Y-m-d");//here you make mistake
     echo $exd1;
?>
-2

Source: https://habr.com/ru/post/1629305/


All Articles