0

i need to convert a date string into this format: "d/m/Y" the date string to convert from, is not always the same, sometimes its 07/02/2014, sometimes 13/02/2014 and sometimes 2014-02-07.

i'm using this command for conversion: date("d/m/Y", strtotime($datestring))

i noticed that sometimes i get 01/01/1970 because of a false that strtotime returns. i narrowed it down to this: strtotime("07/02/2014") returns 1409605200 which is ok.... strtotime("13/02/2014") returns false !!!!

read all the posts about strtotime here , in this forum. also this solution won't help because i don't know the specific format in advance:

$a = strptime('22-09-2008', '%d-%m-%Y'); $timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);

what is the problem here?? what is the solution i need?

Rodniko
  • 4,926
  • 20
  • 69
  • 93
  • If *you* don't even know the time format, how is `strtotime` magically supposed to? It simply can't guess correctly every time. – deceze Feb 07 '14 at 08:29
  • still doesn't answer why a 13/02/2014 fails while the 09 day doesn't... – Rodniko Feb 07 '14 at 08:57
  • 2
    Because XX/XX/XXXX is interpreted as M/D/Y. See http://3v4l.org/uYIAu. That obviously fails if the M is 13. Are you sure 07/02/2014 was *really* interpreted "ok"? – deceze Feb 07 '14 at 09:00
  • **possible duplicate of http://stackoverflow.com/q/15676344/67332 / http://stackoverflow.com/q/10306999/67332 / http://stackoverflow.com/q/8554414/67332 / http://stackoverflow.com/q/6692294/67332** – Glavić Feb 07 '14 at 09:11
  • @deceze - yes i'm sure , it returns the above timestamp which transales good afterwards... also i don't knwo which format because sometimes i extract the value from the database in a certain format , sometimes i take the date straight from the UI with a different format. it is still weird, because does it mean that "13/02/2014" cannot be converted in anyway to timestamp? – Rodniko Feb 07 '14 at 09:16
  • 2
    @Rodniko: you are mistaken! Like deceze said, your date `07/02/2014` is interpeted as `mm/dd/yyyy`, see [demo here](https://eval.in/98933). Can you now see that when you format your timestamp `1409605200` as `Y-m-d` it returns `2014-07-02`, where `07` is MONTH, and `02` is DAY?? Or if you format it back to your format `d/m/Y` it returns `02/07/2014`, which is **not** equal to `07/02/2014`... – Glavić Feb 07 '14 at 09:20
  • 1
    What ^ he ^ said. 07/02/2014 is interpreted as *July 2nd 2014*. If that's "ok" then you have a cognitive dissonance for why 13/02/2014 is also supposed to work. You cannot take arbitrary strings of numbers without knowing what they are and expect anyone or anything to simply figure it out, even a human cannot! You need to place more restrictions on the formats you accept and expect. – deceze Feb 07 '14 at 09:27
  • @Glavic - yes, i already agree on that. thank you. the 07/02/2014 passes successfully because it is seen as 02/07/2014. deceze - ok thanks :) – Rodniko Feb 07 '14 at 09:27

1 Answers1

2

You can do it like this:

/** This method will convert a datestring to d/m/Y
* and respect therefore d/m/Y for incoming value
* @params $dateString
* @retun $dateString
*/

function getMyDate($dateString)
{
    $dateString = str_replace('/', '-', $dateString);
    $date = new DateTime($dateString);
    return $date->format('d/m/Y');
}

//----------------------------
$var = '13/02/2014 ';
echo getMyDate($var);
//--> returns: 13/02/2014

$var = '2014-02-07';
echo getMyDate($var);
//--> returns: 07/02/2014

SQL Fiddle: http://ideone.com/TTRDTA

user8675
  • 657
  • 6
  • 13