PHP: Change Date from DD.MM.YYYY to YYYY-MM-DD
Tip by Computer Expert | 2012-08-07 at 19:43
There are different ways to convert a date from one format to another format. In this tip, I will show you several possibilities how you can convert a date that is separated with dots to a date that is separated with hyphens and that is ordered in a differerent way. The separated by hyphens date can be used for example for MySQL queries.
First variant:
date("Y-m-d", strtotime('10.05.2012');
Second variant:
implode('-', array_reverse(explode('.', '10.05.2012')));
Third variant:
$arr = explode('.', '10.05.2012'); $dat = $arr[2].'-'.$arr[1].'-'.$arr[0];
The first variant may lead to problems when the time is less than the 1970 limit or strtotime() does not understand the format of the date.
About the Author
The author has not added a profile short description yet.
Show Profile
Related Topics
PHP: Current Date and Time
Tutorial | 0 Comments
MySQL: Write current Date or Time into Column
Tutorial | 0 Comments
PHP: Determine Week Number
Tutorial | 0 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 0 Comments
JavaScript: Get current Date and Time
Tutorial | 1 Comment
Change Date of File
Tutorial | 0 Comments
Delphi/Lazarus: Determine System Date Format
Question | 1 Answer
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
Here is another way working with preg_replace:
DD.MM.YYYY -> YYY-MM-DD
YYY-MM-DD -> DD.MM.YYYY
If you want to use another character instead of the hyphen - you can adjust the code at this point.
2017-10-02 at 14:53