Hi, I'm trying to convert a date of this format:
12-5-2008 (m d Y) to 2008-12-5 (Y m d) but when I do the conversion it doesn't display the correct date.

I start with:
$my_date = '12-5-2008';
$my_time = strtotime($my_date);

echo date('Y-m-d',$my_time);
'This displays 2017-10-29 for some reason???

echo date('m-d-Y',$my_time);
'This displays 10-29-2017 for some reason???

If I start with $my_date = '2008-12-5'; //(Y-m-d)
Then the conversion works fine.

What am I doing wrong? Thanks!

    One way:

    list($mon, $day, $yr) = explode('-', $my_date);
    $my_time = mktime(0, 0, 0, $mon, $day, $yr);
    

    At this point you should have a good unix-time value which you can use with date(), etc.

      a year later
      alohaaron wrote:

      $my_date = '12-5-2008';
      $my_time = strtotime($my_date);

      echo date('Y-m-d',$my_time);

      I get '2008-05-12' i.e., 12 May 2008: exactly what I'd interpret "12-5-2008" to mean if I interpreted it as a date.

        Write a Reply...