0

I am trying to figure this out, I am not the php developer, but I was tasked with this anyways. So, I have been trying to figure it out. I have been asked to replace the - with commas, and rearrange the dates. For example mm-dd-yyy to yyyy-mm-dd

This is how I replace the -

$TheDate = "09-09-2013";
$TheDate = str_replace('-', ',', $TheDate);
echo $TheDate;

Now, the problem is that I don't even know where to start on how to rearrange the numbers. Could someone please lead me in the right direction?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Divern
  • 343
  • 2
  • 15

2 Answers2

3

Use php date() and strtotime() to do this. It's the slandered way.

echo date('Y-m-d',strtotime('09-09-2013'));

Output:- https://eval.in/807312

Reference:- PHP date formats

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

This is not the right way to do it, you should use date/time classes but here goes anyway:

$dateArray = explode('-', $TheDate);
$myDate = $dateArray[2] . '-' . $dateArray[0] . '-' . $dateArray[1];

You shouldn't do it this way though because it's not flexible to the user locale or lots of other things that could go wrong.

chris85
  • 23,846
  • 7
  • 34
  • 51
David Findlay
  • 1,296
  • 1
  • 14
  • 30