Convert a date from yyyy-mm-dd to dd-mm-yyyy format

How do I Convert Date Format in PHP

This tutorial uses PHP strtotime() and date() functions to convert date time format. For example you have stored a date YYYY-MM-DD format in a variable and need to change this to MM-DD-YYYY format.

We can achive this by converting date first to seconds using strtotime() function. After that reconstruct date to any format using date() function. Below is few examples of conversion:

1. Change YYYY-MM-DD => MM-DD-YYYY

Here we have date yyyy-mm-dd (“2019-01-15”) format and converting it to mm-dd-yyyy (“01-15-2019”) format.

2. Change YYYY-MM-DD => DD-MM-YYYY

Here we have date yyyy-mm-dd (“2019-01-15”) format and converting it to dd-mm-yyyy (“15-01-2019”) format.

3. Change DD/MM/YYYY => YYYY-MM-DD

If you have slashes in date format like “15/01/2019” and need to convert / with hyphens (-). The following example will help you to convert DD/MM/YYYY (“15/01/2019”) to YYYY-MM-DD (2019-01-15).

Читайте также:  Эффект морской волны css

How to Disable Functions in PHP

Converting UTC Date and Time to Local Time in Linux

Getting Yesterday’s Date in Bash: A Practical Walkthrough

10 Comments

Hello Paul, this is a great tutorial but I have question regarding my current problem in MySQL and PHP in finding the difference between 2 dates(variables: start date and end date). MySQL date format is YYYY-mm-dd while in bootstrap is mm/dd/YYYY. I want to calculate the difference between two dates from MySQL and display the output from PHP. I hope you could help me on this Paul ?

what do i do if i want to convert any format into a specified format
whether it is in string format i.e february or feb
whether it is 2 or 02
whether it is short form of year i.e 1973 or 73 or format listed in
https://docs.oracle.com/cd/E41183_01/DR/Date_Format_Types.html

Great article however what do you do if the original date is in the format of a posted string ie let’s say “15/01/2019” . $origDate = $_POST[‘Date1’]; $date = str_replace(‘/’, ‘-‘, $origDate );
$newDate = date(“Y-m-d”, strtotime($date));
echo $newDate;

Источник

How to convert a date from yyyy-mm-dd to dd-mm-yyyy format in PHP?

We can convert a date from yyyy-mm-dd to dd-mm-yyyy format by using the PHP strtotime() and date() function.

The strtotime() function is a predefined function offered by the PHP, which converts the string date and time into a UNIX timestamp then by using the date() function, we can convert the timestamp into the desired date format.

Example: Format using strtotime() function

In the given example, we have converted a date from yyyy-mm-dd format to dd-mm-yyyy format using the strtotime() function.

     

Using DateTime() function

We can also convert a date from yyyy-mm-dd to dd-mm-yyyy format by using DateTime() function and used the format function that returns a new formatted date according to the specified format.

Example: Using DateTime() function

In the given example, we have converted a date format from yyyy-mm-dd to dd-mm-yyyy using DateTime() function.

     format('d-m-Y'); ?>  

Conclusion

In this lesson, we have learned how to convert the date format from yyyy-mm-dd to dd-mm-yyyy in PHP. We used a predefined function strtotime() that first converts the date format into the UNIX timestamp, and then using the date() function, we can convert the time stamp into the desired date format.

Источник

date() – форматирование даты PHP

date($format, $timestamp) – форматирует дату/время по шаблону, где:

Результат работы функции зависит от настроек часового пояса, установить его можно следующем образом:

Основные параметры шаблона

Символ Описание Пример возвращаемого значения
День
j День месяца без ведущего нуля от 1 до 31
d День месяца, 2 цифры с ведущим нулём от 01 до 31
l Полное наименование дня недели от Sunday до Saturday
D День недели, 3 символа от Mon до Sun
z Порядковый номер дня в году От 0 до 365
Неделя
N Порядковый номер дня недели от 1 (понедельник)
до 7 (воскресенье)
W Порядковый номер недели года Например: 42 (42-я неделя года)
Месяц
n Порядковый номер месяца без ведущего нуля от 1 до 12
m Порядковый номер месяца с ведущим нулём от 01 до 12
F Полное название месяца от January до December
M Сокращенное наименование месяца, 3 символа от Jan до Dec
t Количество дней в месяце от 28 до 31
Год
Y Год, 4 цифры 2019
y Год, 2 цифры 19
Время
g Часы в 12-часовом формате без ведущего нуля от 1 до 12
h Часы в 12-часовом формате с ведущим нулём от 01 до 12
G Часы в 24-часовом формате без ведущего нуля от 0 до 23
H Часы в 24-часовом формате с ведущим нулём от 00 до 23
i Минуты с ведущим нулём от 00 до 59
s Секунды с ведущим нулём от 00 до 59
v Миллисекунды 123
Полная дата/время
c Дата в формате ISO 8601 2004-02-12T15:19:21+00:00
r Дата в формате RFC 2822 Thu, 21 Dec 2000 16:01:07 +0200

дд.мм.ггг (dd.mm.yyyy)

Самый распространенные форматы: dd.mm.yyyy и dd.mm.yyyy hh:ss .

echo date('d.m.Y'); // 26.07.2023 echo date('d.m.Y H:i'); // 26.07.2023 15:16 echo date('d.m.Y H:i:s'); // 26.07.2023 15:16:34

гггг-мм-дд (yyyy.mm.dd)

Используется в MySQL тип поля DATE.

Источник

How to Convert a Date Format in PHP

Here is a short tutorial that represents how to convert a date format in PHP. As a rule, the built-in functions such as strtotime() and date() are used to achieve the desired conversion.

Check out several possible conversion cases below.

Convert YYYY-MM-DD to DD-MM-YYYY

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

 $orgDate = "2020-05-15"; $newDate = date("d-m-Y", strtotime($orgDate)); echo "New date format is: " . $newDate . " (MM-DD-YYYY)"; ?>

Convert YYYY-MM-DD to MM-DD-YYYY

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

 $orgDate = "2020-05-15"; $newDate = date("m-d-Y", strtotime($orgDate)); echo "New date format is: " . $newDate . " (MM-DD-YYYY)"; ?>

Convert DD-MM-YYYY to YYYY-MM-DD

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

 $orgDate = "15-05-2020"; $newDate = date("Y-m-d", strtotime($orgDate)); echo "New date format is: " . $newDate . " (YYYY-MM-DD)"; ?>

Convert DD-MM-YYYY to YYYY/MM/DD

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

 $orgDate = "15-05-2020"; $date = str_replace('-"', '/', $orgDate); $newDate = date("Y/m/d", strtotime($date)); echo "New date format is: " . $newDate . " (YYYY/MM/DD)"; ?>

Convert 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.

 $date = "05/15/2020 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; ?>

In this snippet, we covered several handy solutions. If you want to get more information and answers to Date/Time issues in PHP, we recommend you also to check out this page.

Источник

PHP Date Exercises : Convert a date from yyyy-mm-dd to dd-mm-yyyy

Write a PHP script to convert a date from yyyy-mm-dd to dd-mm-yyyy.

Sample Solution:

Flowchart: Convert a date from yyyy-mm-dd to dd-mm-yyyy

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource’s quiz.

Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

How do I use PHP to get the current year?

You can use either date or strftime.

On a side note, when formatting dates in PHP it matters when you want to format your date in a different locale than your default. If so, you have to use setlocale and strftime. According to the php manual on date:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

Оцените статью