Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top