如何设置日期时间PHP?

how to set Date Time PHP?

我只有 php 语句的这个字段,这个字段的输出是:August 11 2016 但是我怎样才能使这个 11 08 2016 ?

<?php the_time(get_option( 'date_format' )); ?>

更新

the_time() wordpress 方法接受您想要打印时间的格式参数。所以像这样更新代码以按所需格式打印它。

<?php the_time('d m Y'); ?>

您可以使用createFromFormat to create the DateTime object from string according to the format, and then use format方法以所需格式打印它。

您的代码应如下所示,

<?php
$date_string= "August 11 2016";
$date = DateTime::createFromFormat('F d Y',$date_string);
echo $date->format("d m Y");

演示:https://eval.in/621469

其中 F d Y 是输入日期格式,d m Y 是输出日期格式。

createFromFormat returns new DateTime object formatted according to the specified format passed as first argument.

您指的是在此处找到的 WordPress 函数 the_time()

https://codex.wordpress.org/Function_Reference/the_time

如果是这样,您可以在括号内指定如下格式:

<?php the_time('d m Y'); ?>

如果您想要 PHP 日期的格式选项列表,请查看以下手册页:

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

这是一个简单的解决方案:

echo date('d m Y', strtotime("August 11 2016"));  
  • 首先使用 strtotime
  • 将字符串日期转换为 Unix time-stamp
  • 然后将其转换为您选择的日期格式

Check Demo Here