简体   繁体   中英

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.

I started to write the following code in 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:

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. 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.

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

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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