As strtotime() converts valid date formats to UNIX timestamp. If my input is in dd-mm-yyyy format (e.g. 09-10-2014) and mm-dd-yyyy (e.g. 10-09-2014), how to make sure that strtotime convert it to correct timestamp?
2 Answers
You can use DateTime
<?php
$date = DateTime::createFromFormat('d-m-Y', '05-09-2014');
print_r( $date->getTimestamp() );
$date = DateTime::createFromFormat('m-d-Y', '09-05-2014');
print_r( $date->getTimestamp() );
Live Preview
Comments
If you know exactly input format, you could do something like this:
// for DD-MM-YYYY
$date = '09-05-2014'; // 09 May 2014
$converted = implode('-',array_reverse(explode('-', $date))); // returns 2014-05-09
// for MM-DD-YYYY , as Mark Baker mentioned above
$date = '05-09-2014'; // 09 May 2014
$converted = date('Y-m-d', strtotime(str_replace('-','/', $date))); // returns 2014-05-09
mm-dd-yyyy, change it tomm/dd/yyyy