How to convert string to time format in php

PHPz
Release: 2023-03-27 16:23:28
Original
938 people have browsed it

PHP is a popular programming language widely used in web development. Converting between strings and dates is a very common operation in PHP. In this article, we will discuss how to convert PHP string to time format.

First, let us understand the format of date and time. In PHP, the format for representing dates and times is Y-m-d H:i:s, where Y represents the year, m represents the month, d represents the date, H represents the hour, i represents the minute, and s represents the second. Separate the date and time with a space.

When we get a date and time value from the database, it is usually a string type, such as "2021-05-12 10:30:00". We can use PHP's strtotime() function to convert this string into a timestamp value, and then convert it into date and time format. Here is an example:

$date_string = "2021-05-12 10:30:00";
$time_stamp = strtotime($date_string);
$date_time = date("Y-m-d H:i:s", $time_stamp);
echo $date_time;
Copy after login

In the above example, we first assign the date string to the $date_string variable. We then use the strtotime() function to convert it to a timestamp. Finally, we use the date() function to convert the timestamp into date and time format and output it to the screen.

Sometimes, we need to convert date and time formats into different formats, such as month-day-year. For this purpose we can use PHP's DateTime class. Here is an example:

$date_string = "2021-05-12 10:30:00";
$datetime = new DateTime($date_string);
$formatted_date = $datetime->format('m-d-Y');
echo $formatted_date;
Copy after login

In the above example, we first assign the date string to the $date_string variable. Then, we create a $datetime object using the DateTime class. Finally, we use the format() method to convert the date and time format into "month-day-year" format and output it to the screen.

Of course, you can also use the DateTime class to directly convert date and time into a timestamp. Here is an example:

$date_string = "2021-05-12 10:30:00";
$datetime = new DateTime($date_string);
$time_stamp = $datetime->getTimestamp();
echo $time_stamp;
Copy after login

In the above example, we use the DateTime class to create a $datetime object, and then use the getTimestamp() method to convert it to a timestamp and output it to the screen.

In this article, we learned how to convert PHP string to time format. We discussed using the strtotime() function to convert a string to a timestamp and using the date() function and the DateTime class to convert a timestamp to date and time format. Either way, date and time values ​​can be easily converted and manipulated in PHP.

The above is the detailed content of How to convert string to time format in 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