0

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?

1

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.