Converting a string to a date or a DateTime object in PHP is a common task when working with date and time information. PHP provides several methods and functions for parsing and converting date strings into more manageable formats. In this tutorial you will learn how to convert a string to a date and a DateTime object in PHP.
Converting a String to a Date
To convert a date string to a Date object, you can use the strtotime()
function or the date_create_from_format()
function.
Using “strtotime()” Function
The strtotime() function is a simple way to convert a date string to a Date object. It can parse a wide range of date formats and return a Unix timestamp representing the date. Here’s an example:
$dateString = "2023-10-15"; // Date string in "Y-m-d" format
$date = strtotime($dateString);
if ($date !== false) {
$formattedDate = date("Y-m-d", $date);
echo "Converted Date: $formattedDate";
} else {
echo "Invalid date format";
}
Converted Date: 2023-10-15
In this code:
- We have a date string in the “Y-m-d” format (year-month-day).
- We use the
strtotime()
function to parse the date string and obtain a Unix timestamp. - If the conversion is successful (i.e.,
$date
is notfalse
), we format the date back into the desired format using thedate()
function.
This method is suitable for simpler date conversions.
Using “date_create_from_format()” Function
If you need to convert a string to a DateTime object with a specific format, you can use the date_create_from_format() function. Here’s an example:
$dateString = "10/15/2023"; // Date string in "m/d/Y" format
$date = date_create_from_format("m/d/Y", $dateString);
if ($date !== false) {
$formattedDate = $date->format("Y-m-d");
echo "Converted Date: $formattedDate";
} else {
echo "Invalid date format";
}
In this code:
- We have a date string in the “m/d/Y” format (month/day/year).
- We use the
date_create_from_format()
function to create a DateTime object with the specified format. - If the conversion is successful (i.e.,
$date
is notfalse
), we format the date using theformat()
method.
This method is ideal when you need to convert a date string with a specific format.
Converting a String to a DateTime Object
To convert a string to a DateTime object, you can use the DateTime::createFromFormat()
method. Here’s an example:
$dateString = "2023-10-15 14:30:00"; // Date and time string
$dateTime = DateTime::createFromFormat("Y-m-d H:i:s", $dateString);
if ($dateTime !== false) {
$formattedDateTime = $dateTime->format("Y-m-d H:i:s");
echo "Converted DateTime: $formattedDateTime";
} else {
echo "Invalid date and time format";
}
Converted DateTime: 2023-10-15 14:30:00
Handling Invalid Date Strings in PHP
It’s essential to handle cases where the date string cannot be parsed successfully. Both strtotime() and date_create_from_format() will return false when they fail to parse the input. You should check for this condition to ensure that the conversion was successful. In the examples provided, we included checks to handle invalid date strings.