简体   繁体   English

更改PHP中任意日期的格式

[英]changing the format of a arbitrary date in php

I have a program where I want to enter an arbitrary date 我有一个想要输入任意日期的程序

2016-07-21 00:00:00

and I want to convert the format of the date to July 21 2016 00:00 GMT. 我想将日期格式转换为2016年7月21日格林尼治标准时间。

I started to write the following code in php: 我开始在php中编写以下代码:

#!/bin/php
<?php
   $date = "2016-07-21 00:00:00";
   echo date_format($date, 'F jS Y H:i:s');
?>

Now when I try to use this code, which I got from http://php.net/manual/en/function.date.php I get the following error messages: 现在,当我尝试使用从http://php.net/manual/en/function.date.php获得的这段代码时,收到以下错误消息:

PHP Warning:  date_format() expects parameter 1 to be DateTime, string   given in /home/vrsops/server/work/experimental/exp.php on line 8

What does this mean? 这是什么意思? How do I resolve this issue and be able to convert the date format to the one specified above? 如何解决此问题并能够将日期格式转换为上面指定的格式?

As the error message states date_format() requires a DateTime() object to be formatted. 由于错误消息指出date_format()需要格式化DateTime()对象。 You gave it a string. 您给了它一个字符串。 If you want to use date_format() then you need to create use date_create() to create a DateTime() object. 如果要使用date_format()则需要创建使用date_create()来创建DateTime()对象。

<?php
    $date = date_create("2016-07-21 00:00:00");
    echo date_format($date, 'F jS Y H:i:s');
?>

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM