How to convert string to datetime format using PHP

PHPz
Release: 2023-03-27 17:41:38
Original
1070 people have browsed it

PHP is a commonly used server-side scripting language that is widely used in web development. PHP's processing of strings is quite flexible, making it easy to convert strings into date and time format. This article will introduce how to use PHP to convert a string into date and time format.

  1. Convert a string to a timestamp

In PHP, the strtotime() function can convert a string to a Unix timestamp. A Unix timestamp is the number of seconds since January 1, 1970 UTC. The input string can be converted to a Unix timestamp by passing it to strtotime(). For example, convert the following string to a Unix timestamp:

$dateString = "2021-08-26 22:30:00";
$timestamp = strtotime($dateString);
Copy after login

Now, the $timestamp variable will contain the Unix timestamp of the date and time string.

  1. Format timestamp to datetime

Once the string is converted to a Unix timestamp, you can use the date() function to Format it into the datetime format you need. For example, the following code will convert a datetime string to a Unix timestamp and format it into the US regular datetime format using:

$dateString = "2021-08-26 22:30:00";
$timestamp = strtotime($dateString);
$formattedDate = date("m/d/Y h:i A", $timestamp);
Copy after login

In the above example, $formattedDate will contain a string of the form "08 /26/2021 10:30 PM" date and time string.

  1. Using the DateTime class

PHP also provides a DateTime class that can be used to parse and format dates and times. This class provides many methods for working with dates and times, such as add(), sub() and diff(). Converting strings to datetime format becomes easier using the DateTime class. For example, the following code converts a string to a DateTime object and formats it into the ISO date time format:

$dateString = "2021-08-26 22:30:00";
$dateTimeObj = new DateTime($dateString);
$formattedDate = $dateTimeObj->format('Y-m-d\TH:i:s.uP');
Copy after login

In the above example, $formattedDate will contain something like "2021-08-26T22:30 :00.000000 00:00" date and time string. The DateTime class also makes it easy to perform operations such as date calculations.

Summary

In PHP, converting a string to datetime format is very easy. You can use the strtotime() function to convert the string to a Unix timestamp and then use the date() function to format it into the format you need. Alternatively, you can use PHP's DateTime class to parse and format dates and times. Using these tools, you can easily convert strings to various date-time formats, making it easier to integrate with other systems and APIs.

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

Related labels:
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