You are given a string which contain date and time. Date in dd/mm/yyyy format and time in 12 hrs format.You have to convert date in yyyy/mm/dd format and time in 24 hrs format.
Examples:
Input : $date = "12/05/2018 10:12 AM" Output : 2018-05-12 10:12:00 Input : $date = "06/12/2014 04:13 PM" Output : 2014-12-06 16:13:00
First we will convert date to unix timestamp using strtotime() and then use date() to convert it to a specific format(The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT))
<?php // function to convert string and print function convertString ($date) { // convert date and time to seconds $sec = strtotime($date); // convert seconds into a specific format $date = date("Y-m-d H:i", $sec); // append seconds to the date and time $date = $date . ":00"; // print final date and time echo $date; } // Driver code $date = "06/12/2014 04:13 PM"; convertString($date); ?> |
2014-06-12 16:13:00
Recommended Posts:
- How to change the date format using AngularJS ?
- How to format current date in MM/DD/YYYY HH:MM:SS format using JavaScript ?
- Format a Date using ng-model in AngularJS
- PHP date() format when inserting into datetime in MySQL
- How to get Month and Date of JavaScript in two digit format ?
- How to set input type date in dd-mm-yyyy format using HTML ?
- How to check the input date is equal to today's date or not using JavaScript ?
- D3.js format() Function
- PHP | Format Specifiers
- PHP | DateTime format() Function
- PHP | IntlDateFormatter format() Function
- How to format a float in JavaScript ?
- Express.js | res.format() Function
- How to format a number as currency using CSS ?
- Node.js | path.format() Method
- Format a number with leading zeros in PHP
- How to display a list in n columns format ?
- Node.js util.format() Method
- How do you display JavaScript datetime in 12 hour AM/PM format?
- How to print an array in table format using angularJS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

