Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

PHP reverse date using strtotime

David Carr

Tutorials PHP & MySQL

I use datepickers a lot in my projects the date’s default to the American YYYY-MM-DD format so they store in the database, the trouble is my clients are UK based so it’s not very readable. The datepickers let you set the format you want so I use DD-MM-YYYY.

That’s great until it needs to go into a MySQL database then I need the former date. As I know what the format will always be using strtotime makes it easy to convert like this:

$date = '26-07-2016';
echo date("Y-m-d",strtotime($date));

Using date specify the format desired then pass in strtotime with the date to be formatted.

This is a very easy method, works with times too.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.