PHP format date

廉价感情. 提交于 2019-12-04 04:56:07

问题


How can I force the date format to output:

12/12/2012, 1/10/2012, 1/5/2012

instead of

12/12/2012, 01/10/2012, 01/05/2012?

My code is the following:

$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'd/m/Y');
$adateto = date_format($adateto, 'd/m/Y');

Please do note that I have to format the date AFTER posting it.


回答1:


Have a look at the PHP built in date function here

You will find that your solution is as simple as this:

date('j/n/Y',strtotime($_POST['datefrom']));

The key things to note are the characters used in the first parameter.

  • j represents the day without leading zeros
  • n represents the month without leading zeros

There are many other options you have, just have a read through the documentation.

Please note that a simple search of 'PHP date' on Google would have found this solution for you




回答2:


$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'j/n/Y');
$adateto = date_format($adateto, 'j/n/Y');

you are welcome! ;)



来源:https://stackoverflow.com/questions/14441735/php-format-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!