In PHP, you can change the date format using the date() function or the DateTime class. The date() function allows you to format a timestamp or a current date in various ways. The DateTime class provides more advanced features for working with dates and times.

change-date-format-in-php

Here are examples of both approaches:

.Using the date() function:

$timestamp = time(); // Current timestamp

// Convert timestamp to desired date format

$formatted_date = date("Y-m-d H:i:s", $timestamp); // Example format: 2023-08-08 15:30:00

echo $formatted_date;

1) Using the DateTime class:

$timestamp = time(); // Current timestamp


// Create a DateTime object from timestamp
$date = new DateTime();

$date->setTimestamp($timestamp);

// Format the date using DateTime format method
$formatted_date = $date->format("Y-m-d H:i:s"); // Example format: 2023-08-08 15:30:00
echo $formatted_date;

Here are some common format characters you can use in the format string:

Y - Year with 4 digits (e.g., 2023)

m - Month with leading zeros (01 to 12)

d - Day of the month with leading zeros (01 to 31)

H - Hour in 24-hour format with leading zeros (00 to 23)

i - Minutes with leading zeros (00 to 59)

s - Seconds with leading zeros (00 to 59)

You can combine these characters to create your desired date format.

For example, if you want the date in a more readable format like "August 8, 2023, 3:30 PM", you can use the following format string:

$formatted_date = date("F j, Y, g:i A", $timestamp); // Example format: August 8, 2023, 3:30 PM

Remember that the available format characters may vary depending on the version of PHP you're using.

Change YYYY-MM-DD to DD-MM-YYYY

Imagine, that you have kept date 2023-08-08 in MM-DD-YYYY format and intend to change it to 08-08-2023 in the DD-MM-YYYY format.

Here is an example:

<?php
$orgDate = "2023-08-08";$newDate = date("d-m-Y", strtotime($orgDate));echo "New date format is: " . $newDate . " (MM-DD-YYYY)";
?>

Change YYYY-MM-DD to MM-DD-YYYY

Now, imagine having a date 2023-08-08 in YYYY-MM-DD format. Let’s convert it to 08-08-2023 (MM-DD-YYYY) like this:

<?php

 $orgDate = "2023-08-08";

 $newDate = date("m-d-Y", strtotime($orgDate));

 echo "New date format is: " . $newDate . " (MM-DD-YYYY)";

?>

Change DD-MM-YYYY to YYYY-MM-DD

In this section, consider having a date 08-08-2023 in DD-MM-YYYY format. Below, you can see how to convert it to 2023-08-08 (YYYY-MM-DD) format:

<?php

$orgDate = "08-08-2023";$newDate = date("Y-m-d", strtotime($orgDate));echo "New date format is: " . $newDate . " (YYYY-MM-DD)";

?>

Change DD-MM-YYYY to YYYY/MM/DD

Now, suppose having a date 08-08-2023 in DD-MM-YYYY format. It is separated by dash(-)sign. Let’s try converting it to 2023/08/08 (YYYY/MM/DD) format, separated byslash(/).

Here is how to do that:

<?php

 $orgDate = "08-08-2023";

 $date = str_replace('-"', '/', $orgDate);

 $newDate = date("Y/m/d", strtotime($date));

 echo "New date format is: " . $newDate . " (YYYY/MM/DD)";

?>

Change Date Time to A Different Format

In this section, you will figure out how to transform date format MM-DD-YYYY to YYYY-DD-MM, as well as12 hours time clock to 24 hours.

Here is an example:

<?php

 $date = "08/08/2023 5:36 PM";

 //converts date and time to seconds

 $sec = strtotime($date);

 //converts seconds into a specific format

 $newdate = date("Y/d/m H:i", $sec);

 //Appends seconds with the time

 $newdate = $newdate . ":00";

 // display converted date and time

 echo "New date time format is: " . $newDate;

?>

PHP Convert String to Datetime

This article will teach you how to convert a simple string to a date format using various PHP methods and techniques.

The Basics

To convert a string to date, we will use two functions: strtotime() and the date() functions. We can also use the DateTime::createFromFormat method.

We will start with the combination of strtotime() and date() functions.

PHP strtotime() Function

The PHP strtotime() method allows you to convert a human-readable time format to Unix time.

Unix time refers to the number of seconds elapsed since 1/1/1970 00:00:00.

The function takes a syntax as shown below:

strtotime(string $datetime)

The function takes the timestamp as the argument and returns the date as an integer if the conversion is successful. The function also returns a Boolean false on fail.

NOTE: The function will interpret various formats differently. For example, if you use the separator as a slash, the method will assume the American date and time format as M/D/Y. However, if the separator is a (-), the European format is used (D-M-Y).

To stay on the safe side, use the ISO 8601 (YYYY-MM-DD) format.

Take a look at the following examples that show how to use the strtotime() function.

<?php

    echo strtotime("now"), "\n"; // <-- convert time now to unix

    echo strtotime("next Friday"), "\n";

    echo strtotime("+7 weeks"), "\n";

    echo strtotime("2023-08-08"), "\n";

    echo strtotime("+10 hours"), "\n";

?>

The above examples convert various strings to their relevant Unix timestamps.

PHP Date() Function

The following function we will look at is the Date() function. This function takes a Unix timestamp and converts it to a local date and time format. The syntax of the function can be expressed as:

date(format, timestamp);

It accepts a date and time format and a timestamp. Once formatted, the function returns the date and time formatted accordingly.

For example, let us convert a specific timestamp to a date and time.

<?php

    echo date("d/M/Y H:i:s", 1638092852), "\n";

?>

In the example above, we use the date format to convert a specific Unix timestamp to date/Month/Year Hours:minutes seconds format.

Run the code above, and you should get an output as shown below:

$ php date_format.php

28/Nov/2021 11:47:32

Combining Strtotime() and Date()

To convert a specific string to date and time format, we can combine the strtotime() function and date as we have already seen.

Consider the example shown below:

<?php

    $timestamp = strtotime("+10 hours");

    echo date("d/M/Y H:i:s", $timestamp), "\n";

?>

The example below uses the strtotime() function to convert a specific string to a Unix timestamp.

We then use the date function to convert the timestamp to a specific date and time format.

Use DateTime::createFromFormat()

The other method we can use to create a DateTime from a string is the DateTime::createFromFormat(). This is a built-in PHP function available in PHP 5.2 and higher. It returns a DateTime object representing the date and time in a specified format.

The example below shows how to use it.

<?php

    $datenew = DateTime::createFromFormat("Y-m-d", "2023-08-08");

    echo $datenew->format("Y-m-d"), "\n";

?>

The above code should return the date.