- How to convert DateTime to String in PHP
- The Format method from DateTime
- DateTime to String example
- Php class datetime to string
- PHP String Based
- PHP Class Based
- PHP JSON Based
- PHP File Systems Based
- How to Convert DateTime to String in PHP
- How to convert a DateTime object to a string
- Conclusion
- How to convert DateTime to String in PHP
- The Format method from DateTime
- DateTime to String example
How to convert DateTime to String in PHP
In this article, we will learn how to convert a Date or a DateTime into a String in PHP.
The Format method from DateTime
To convert a Date or a DateTime object into a String in PHP, we can use the format method from the DateTime class. As the name already says, the format method can help us formatting a DateTime object into multiple different formats.
To do so, we simply need to specify the desired format as an argument when calling the format method. That’s it!
In the following section, we will see a practical example of how to use the format method to return a string according to the given format. This method is supported from PHP 5 >= 5.2.1 to the latest version.
DateTime to String example
Let’s now take a look at a practical example. In the code below, we will use format to simply convert a DateTime object into different String formats:
format( 'd/m/Y, H:i:s' ); echo 'datetime: ' . $date_time . '
'; $date = $current_datetime->format( 'd/m/Y' ); echo 'date: ' . $date . '
'; $time = $current_datetime->format( 'H:i:s' ); echo 'time: ' . $time . '
'; $day = $current_datetime->format( 'd' ); echo 'day: ' . $day . '
'; $month = $current_datetime->format( 'm' ); echo 'month: ' . $month . '
'; $year = $current_datetime->format( 'Y' ); echo 'year: ' . $year . '
';
In the example below, we used the DateTime class to initialize the $current_datetime variable with a new DateTime object and using a string. Subsequently, we used the format method to convert the DateTime object into the following formats:
- DateTime: using the ‘d/m/Y, H:i:s’ pattern
- Date: using the ‘d/m/Y’ pattern
- Time: using the ‘H:i:s’ pattern
- Day: using the ‘d’ pattern
- Month: using the ‘m’ pattern
- Year: using the ‘Y’ pattern.
If we run the code below, the output is:
datetime: 17/04/2021, 17:29:15 date: 17/04/2021 time: 17:29:15 day: 17 month: 04 year: 2021
External resources:
Php class datetime to string
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
PHP String Based
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
PHP Class Based
PHP JSON Based
PHP File Systems Based
How to Convert DateTime to String in PHP
You can represent an exact date and time in PHP using the built-in DateTime object.
Creating one is simple, just pass it a string in the format YYYY-MM-DD HH:MM:SS .
The problem is that sometimes you want to go from a DateTime object back to a string.
In this post, we’ll learn how to convert a DateTime object to a string in PHP.
How to convert a DateTime object to a string
To start, let’s create our own DateTime object.
The easiest way to convert this back to a readable string is to call the format method on it and pass it the format in which you want to get the string in.
format('Y-m-d H:i:s'); echo($results);
The useful part about format is that if the format fails because the DateTime object or the format you passed in is invalid, it will return false .
Because of this, you can use this check to ensure the formatting was succesful:
format('Y-m-d H:i:s'); if ($results) < echo($results); >else
Conclusion
In this post, we learned how to convert a DateTime object to a string.
Simply call the format method on your DateTime object and pass the method the date format you want your DateTime formatted to.
Thanks for reading and happy coding!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Give feedback on this page , tweet at us, or join our Discord !
How to convert DateTime to String in PHP
In this article, we will learn how to convert a Date or a DateTime into a String in PHP.
The Format method from DateTime
To convert a Date or a DateTime object into a String in PHP, we can use the format method from the DateTime class. As the name already says, the format method can help us formatting a DateTime object into multiple different formats.
To do so, we simply need to specify the desired format as an argument when calling the format method. That’s it!
In the following section, we will see a practical example of how to use the format method to return a string according to the given format. This method is supported from PHP 5 >= 5.2.1 to the latest version.
DateTime to String example
Let’s now take a look at a practical example. In the code below, we will use format to simply convert a DateTime object into different String formats:
format( 'd/m/Y, H:i:s' ); echo 'datetime: ' . $date_time . '
'; $date = $current_datetime->format( 'd/m/Y' ); echo 'date: ' . $date . '
'; $time = $current_datetime->format( 'H:i:s' ); echo 'time: ' . $time . '
'; $day = $current_datetime->format( 'd' ); echo 'day: ' . $day . '
'; $month = $current_datetime->format( 'm' ); echo 'month: ' . $month . '
'; $year = $current_datetime->format( 'Y' ); echo 'year: ' . $year . '
';
In the example below, we used the DateTime class to initialize the $current_datetime variable with a new DateTime object and using a string. Subsequently, we used the format method to convert the DateTime object into the following formats:
- DateTime: using the ‘d/m/Y, H:i:s’ pattern
- Date: using the ‘d/m/Y’ pattern
- Time: using the ‘H:i:s’ pattern
- Day: using the ‘d’ pattern
- Month: using the ‘m’ pattern
- Year: using the ‘Y’ pattern.
If we run the code below, the output is:
datetime: 17/04/2021, 17:29:15 date: 17/04/2021 time: 17:29:15 day: 17 month: 04 year: 2021
External resources: