PHP date_format(): How to format date from string value

家住魔仙堡 提交于 2019-12-05 13:47:40

Use createFromFormat method first, provide 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;

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

check the function date_create_from_format() its also a alias function of 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;

It can be a date function call simply. Use stringtotime for exact/precise date/time value

date("Y-m-d",strtotime("01 Dec 2015"))

When you run this code the out put will show

2015-12-01

this is because of the comma in the string which terminates the date string in the compiler. If you specify exactly the timezone (like $timezone = 'America/New_York) . parameter you can show precise time as well.

i got the solution of your bug that is 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;
?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!