formatting a php date/time stamp php 5.2

Hey folks,

I have an interesting issue. I have a function for converting mm/dd/yyyy to database input format of yyyy-mm-dd (see attached code).

However, it only "half" works. For example, if I enter 04/15/2010, it comes out as 2011-03-04. Yet if I enter 04/01/2010, it comes out as expected, 2010-04-01.  So, where the heck did I go wrong to yield the inconsistencies?


<?php

$date=trim($_POST['dob']);
list($d, $m, $y) = explode('/', $date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$formatted_date=strftime('%Y-%m-%d',$mk);
echo $formatted_date;

?>

Open in new window

jmoriartyAsked:
Who is Participating?
 
KysolCommented:
Change:

list($d, $m, $y) = explode('/', $date);

To:

list($m, $d, $y) = explode('/', $date);
0
 
tdotCommented:
Try this ..


<?php

$date=trim($_POST['dob']);
list($d, $m, $y) = explode('/', $date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$formatted_date=date('Y-m-d',$mk);
echo $formatted_date;

?>
0
 
jmoriartyAuthor Commented:
Same result, 04/15/2010 yields an output of 2011-02-04.

Thanks for the help in any case.
0
 
jmoriartyAuthor Commented:
Oh geez, don't you love it when it's something simple staring you square in the face?

Nice catch, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.