-1

I have some short dates in my POST variables.

If I do this:

$i = 0;
foreach ($data->week as $week) {
    $date = $_POST['date'.($i+1)];
    echo $date;
    ...
    $i++;
 }

it returns the correct short dates e.g. 09.12. and 12.12.

If I do this

echo date('d.m.Y', strtotime($date));

it's returning 09.12.2013 (correct) and 09.12.2013 (incorrect, should be 12.12.2013).

Andy ideas?

Keith L.
  • 2,084
  • 11
  • 41
  • 64
  • Why don't you do `$date . "." . date(Y);`? it joins the day/month with the year – Francisco Presencia Dec 09 '13 at 10:49
  • "09.12" is a terrifically ambiguous, incomplete, vague date. You cannot expect `strtotime` to know what you mean by that. Use any of the available methods to explicitly parse a date in a specified format (e.g. `DateTime::createFromFormat`). – deceze Dec 09 '13 at 10:49

2 Answers2

2

Your usage for strtotime isn't correct. With such call it will apply time value for current date. It's like:

var_dump(date('d.m.Y H:i:s', strtotime('12.12')));//09.12.2013 12:12:00
var_dump(date('d.m.Y H:i:s', strtotime('11.12')));//09.12.2013 11:12:00

Instead you should use DateTime API with it's createFromFormat() method.

Alma Do
  • 37,009
  • 9
  • 76
  • 105
1

Is something stopping you from using the DateTime class ? ;)

Do something like this

<?php
$dt = '12.12';
$ctime = DateTime::createFromFormat('d.m', $dt);
echo $ndate= $ctime->format('d.m.Y'); // "prints" 12.12.2013
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126