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
Achrome
  • 7,773
  • 14
  • 36
  • 45
Faishal
  • 1,493
  • 1
  • 14
  • 23
  • 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 http://www.php.net/manual/en/datetime.formats.php and to http://www.php.net/manual/en/datetime.createfromformat.php – Mark Baker Feb 20 '13 at 07: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. – deceze Feb 20 '13 at 07:51
  • alias of http://stackoverflow.com/questions/2222851/convert-string-to-date-in-php – Ripa Saha Feb 20 '13 at 07:57

3 Answers3

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
sectus
  • 15,605
  • 5
  • 55
  • 97
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.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
-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

Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
  • 4
    Why throw somebody who's already using DateTime objects back to using the more problematic date functions? – Mark Baker Feb 20 '13 at 07:51