Converting a date format in PHP can sometimes be overwhelming but using PHP's native date function, it becomes much easier once you've established PHP's basic date principles. A common developer's requirement is converting a date in PHP for use within their application. Let's explore how you can quickly and easily convert a date format in PHP.
What is a date format in PHP?
The best way to convert a date format in PHP is by using PHP's date() function. A date format in PHP is represented by the use of letters which get converted into a date following the guidelines set out by PHP. Each letter represents a part of a time and date and can be used to format a date in PHP you way you wish. Using this convention, allows us to fully manipulate dates in a way that will be useful for our application. The example below shows us the default date format in PHP using a procedural style. Letter case is important here, as some values use the same letter but changes depend on the letter's case. Each letter provided to the PHP date() function represents the following;
- "Y" - prints the year (capital letter)
- "m" - prints the month
- "d" - prints the day
- "H" - prints the hour (capital letter)
- "i" - prints the minute
- "s" - prints the second
echo date("Y-m-d H:i:s");
# Outputs
2024-04-29 11:20:36
Any unrecognised characters will print as is, which includes dashes and colons ("-", ":"). There are many more date formats available to developers, and the full list can be found within the date function in the PHP documentation.
Convert a Date Format in PHP Solution
By using PHP's date function, we can combine it with PHP's strtotime function. Using this function allows us to take any English date/time and convert it into a Unix timestamp. Once converted to a Unix timestamp we can convert it back into a new format. Unix timestamps are dates used by computers to determine time past (in seconds) since midnight on 1st January 1970. We have a handy tool that lets you convert timestamps into human-readable dates. Let's say we want to convert an existing date's format to have the day first instead of the year.
$currentDate = "2024-04-29";
# Convert to Unix timestamp
$unixTimestamp = strtotime($currentDate);
# Convert date to day, month, year format
echo date('d-m-Y', $unixTimestamp);
After running the following code, our old date (yyyy-mm-dd) has now been converted following our new day, month, and year format (dd-mm-yyyy).
29-04-2024
Because we're telling PHP we only want to print out the date, you'll notice there's no time. The time is still available to us, if we want it, we have to ensure we tell PHP's date function to print it out. The example below takes our converted date, but this time printing the time too.
echo date('d-m-Y H:i:s', $unixTimestamp);
# Outputs
29-04-2024 00:00:00
It prints midnight by default because our original date didn't have a time. If no time is provided, it defaults to midnight. If you need the time converted, ensure it's provided on the original date.