Home > Backend Development > PHP Problem > How to convert php datetime to string

How to convert php datetime to string

藏色散人
Release: 2023-03-12 09:00:02
Original
3002 people have browsed it

How to convert php date and time to string: 1. Create a PHP sample file; 2. New a DateTime; 3. Use "$datetime->format('jS, F Y')" to Just convert the date and time into a string.

How to convert php datetime to string

The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to convert php date and time for string?

PHP uses the DateTime class to convert time and date to string

PHP’s processing of time and date is not very standardized. It’s simple. It’s just that I don’t know whether the input string can be Correctly converted to the required DateTime type.

Object-oriented PHP should use the DateTime class to convert string and dateTime

From string to time type

DateTime::createFromFormat(‘m/d/Y H:i','03/01/2008 02:20');
 $totalPrice=0.0;
Copy after login

From DateTime to string

$datetime = new DateTime('2008-08-03 14:52:10');
echo $datetime->format('jS, F Y') . "/n";
Copy after login

Get the current time

getdate()
Copy after login

Although this operation is a bit troublesome, each part is in a format that is precisely controlled by myself, so I feel more at ease.

The above are only for PHP 5.3 and above

Old versions still need to use this

echo date('m/d/Y H:i',strtotime('10/20/2009 15:21'));
Copy after login

Note: What is read from the database using an array is a character type

Recommended: "PHP Video Tutorial"

The above is the detailed content of How to convert php datetime to string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template