2

How to get date format of given string, string contain a valid date I am try to import data from csv file, and that csv files are exported for backup but there are so many different type of date format and in some case when i try to convert string to date it throwing me an error

ex: 04/08/2010 10:22 am

some time this type of format throwing me error

Error 500: DateTime::__construct(): Failed to parse time string
3
  • 3
    So is 04/08/2010 the 4th of August, or the 8th of April? A little bit of reading about DateTime objects in the manual might have pointed you to php.net/manual/en/datetime.formats.php and to php.net/manual/en/datetime.createfromformat.php Commented Feb 20, 2013 at 7:50
  • There's hardly an automatic way to do that. DateTime already tries to identify various date formats automatically, it just fails on ones it doesn't know. You'll need to prepare a list of possible formats and probably test against them with a Regex. Commented Feb 20, 2013 at 7:51
  • alias of stackoverflow.com/questions/2222851/… Commented Feb 20, 2013 at 7:57

3 Answers 3

3

You can use just strtotime

echo date('Y-m-d H:i:s', strtotime('04/08/2010 10:22 am')); // output: 2010-04-08 10:22:00
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the DateTime::createFromFormat() static method.

Also, you may want to do some reading:

Also, you need to use the search function (top right on the page). There are a lot of questions that deal with similar problems.

Comments

-1

Try this...........

           $today = date("d/m/Y g:i a");

<?php 
    $today = date("d/m/Y g:i a");
    echo $today;
?>

You will get output like this.........20/02/2013 7:53 am

For sample one see this example link

1 Comment

Why throw somebody who's already using DateTime objects back to using the more problematic date functions?

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.