0
0
PhpHow-ToBeginner · 2 min read

PHP How to Convert String to Date Easily

Use new DateTime('your-string') or date('Y-m-d', strtotime('your-string')) to convert a string to a date in PHP.
📋

Examples

Input2024-06-15
Output2024-06-15 00:00:00
InputJune 15, 2024
Output2024-06-15 00:00:00
Input15/06/2024
Output2024-06-15 00:00:00
🧠

How to Think About It

To convert a string to a date in PHP, you first recognize the string format and then use PHP's built-in DateTime class or strtotime function to parse it into a date object or formatted string. This lets you work with dates easily in your code.
📐

Algorithm

1
Get the input string representing the date.
2
Use DateTime constructor or strtotime to parse the string.
3
If using DateTime, create a DateTime object from the string.
4
If using strtotime, convert the string to a timestamp and then format it.
5
Return or print the date in the desired format.
💻

Code

php
<?php
$dateString = 'June 15, 2024';
$date = new DateTime($dateString);
echo $date->format('Y-m-d H:i:s');

// Alternative using strtotime
$timestamp = strtotime($dateString);
echo "\n" . date('Y-m-d H:i:s', $timestamp);
?>
Output
2024-06-15 00:00:00 2024-06-15 00:00:00
🔍

Dry Run

Let's trace converting 'June 15, 2024' to a date using DateTime and strtotime.

1

Create DateTime object

Input string: 'June 15, 2024' -> DateTime object representing 2024-06-15 00:00:00

2

Format DateTime

DateTime object formatted to 'Y-m-d H:i:s' -> '2024-06-15 00:00:00'

3

Use strtotime

strtotime('June 15, 2024') returns timestamp for 2024-06-15 00:00:00

4

Format timestamp

date('Y-m-d H:i:s', timestamp) -> '2024-06-15 00:00:00'

StepActionValue
1DateTime object created2024-06-15 00:00:00
2Formatted output2024-06-15 00:00:00
3Timestamp from strtotime1718448000
4Formatted date from timestamp2024-06-15 00:00:00
💡

Why This Works

Step 1: Parsing the string

The DateTime constructor reads the string and converts it into a date object internally.

Step 2: Formatting the date

Using format() on the DateTime object outputs the date in a readable format like 'Y-m-d H:i:s'.

Step 3: Using strtotime

strtotime() converts the string to a Unix timestamp, which is a number representing seconds since 1970-01-01.

Step 4: Converting timestamp to date

The date() function formats the timestamp back into a human-readable date string.

🔄

Alternative Approaches

Using DateTime::createFromFormat
php
<?php
$dateString = '15/06/2024';
$date = DateTime::createFromFormat('d/m/Y', $dateString);
echo $date->format('Y-m-d');
?>
Use this when the date string has a specific known format different from default parsing.
Using strtotime with direct formatting
php
<?php
$dateString = '2024-06-15';
echo date('d-m-Y', strtotime($dateString));
?>
Quick and simple for common date formats but less flexible for complex formats.

Complexity: O(1) time, O(1) space

Time Complexity

Parsing a date string and formatting it is a constant time operation since it involves fixed steps without loops.

Space Complexity

Only a few variables and objects are created, so space usage is constant.

Which Approach is Fastest?

Using strtotime with date is slightly faster but less flexible than DateTime, which is better for complex formats and error handling.

ApproachTimeSpaceBest For
DateTimeO(1)O(1)Complex formats, error handling
strtotime + dateO(1)O(1)Simple, common formats
DateTime::createFromFormatO(1)O(1)Known custom formats
💡
Use DateTime for more control and better error handling when converting strings to dates.
⚠️
Trying to parse date strings without specifying the format can lead to wrong dates if the string format is ambiguous.