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?

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Divern
  • 325
  • 2
  • 14

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

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
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,591
  • 7
  • 30
  • 47
David Findlay
  • 1,148
  • 1
  • 12
  • 30