0

how could i convert this exemple of string values array:

$taba=array('12/04/12','13/05/13','03/01/12');

to date type values, sort these dates chronogically before returning them as strings in a select option HTML input ?

3
  • So converting to some kind of date type is not really required, but rather your thoughts on how sorting could be accomplished? Commented Oct 17, 2013 at 12:48
  • What do you mean by date type values ? Unix int or other ? Commented Oct 17, 2013 at 12:51
  • Why you didnt accepted my answer? its correct, you accepted answer, which takes wrong date format 03/01/12, read comment below his answer... Commented Oct 17, 2013 at 13:33

6 Answers 6

4

Here you are (sorted from past to the future):

$taba=array('12/04/12','13/05/13','03/01/12');
$tabb=array();

foreach ($taba as $key => $val) {
    $d = explode('/', $val);

    $tabb[$key] = $d[2].'-'.$d[1].'-'.$d[0];
}

asort($tabb);

echo '<select>';
    foreach ($tabb as $key => $val) {
        echo '<option value="'.$val.'">'.$taba[$key].'</option>';
    }
echo '</select>';
Sign up to request clarification or add additional context in comments.

5 Comments

Why do you need $key => $val ?
because he wanted to have it written in his string format (d/m/Y), but sorted through Y-m-d format. You have to have there two arrays. First check, then minus.
You can not use key-value for $taba, it's not an associative array.
@JohnSmith: That's incorrect. You can use $key => $value for non-associative arrays. See 3v4l.org/CY3KA
Ahh, okay. But still why will I do it when I can avoid it? Sorry
3

You can use array_map to get a timestamp for each input date, then array_multisort to reorder the dates based on the sorted order of the corresponding timestamps:

$taba=array('12/04/12','13/05/13','03/01/12');
$timestamps = array_map(function($d) {
    return DateTime::createFromFormat('d/m/y', $d)->getTimestamp(); }, $taba);
array_multisort($timestamps, $taba);

print_r($taba);

See it in action.

Comments

3

As variant, you can use strtotime() for converting each element to timestamp, than sort how you need and transform back before output. Example:

$taba=array('12/04/12','13/05/13','03/01/12');
usort($taba, function($a, $b){
    $ta = strtotime(str_replace('/', '-', $a));
    $tb = strtotime(str_replace('/', '-', $b));
    if ($ta == $tb) {
        return 0;
    }
    return ($ta < $tb) ? -1 : 1;
});
print_r($taba);

2 Comments

The only problem being that strtotime will not parse this particular format correctly...
Try adding at strtotime() str_replace("/","-",$var) because current format can't be parsed correctly. when there is / I think its m/d/Y .
2
$taba=array('12/04/12','13/05/13','03/01/12');
function todate($date){
     // as Legionar comment your date format is wrong .. can't guess which is date, which is year..
     // if year is the last numbers you must change the return to ..
     // return strtotime(implode("-",array_reverse(explode("/",$date))));
     return strtotime(str_replace("/","-",$date));
}

$map = array_map('todate', $taba);
sort($map);
echo "<select>";
foreach($map as $key=>$value) {
    echo '<option>'.date("Y-m-d H:i:s", $value)."</option>";
}
echo "</select>";

Sorry I missed the sort point. Here is with sorting :) You can generate the wished date format at the foreach date function..

2 Comments

Your code is wrong! Your function todate takes $taba in format Y/m/d, but his format is d/m/Y. I.e. your answer takes 03/01/12 as 12th January 2003, but it is 3rd January 2012...
You are right. But why you are sure that its not 12-01-2003 ? Its not correct format.. only the code developer can know what will come to him..
2

If you're not worried about invalid dates and all dates have a consistent format, might I suggest you use usort with a custom compare function that sorts the dates however you wish e.g.:

function cmp($a, $b) {
  $year = strcmp(substr($a, -2), substr($b, -2));
  if ($year == 0) {
    // years are the same, try month
    $month = strcmp(substr($a, 3, 2), substr($b, 3, 2));
    if ($month == 0) {
      return strcmp(substr($a, 0, 2), substr($b, 3, 2));
    }
    return $month;
  }
  return $year;
}

Comments

2
$taba=array('12/04/12','13/05/13','03/01/12');

usort(
    $taba,
    function ($valueA, $valueB) {
        return preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$3$2$1', $valueA) > 
            preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$3$2$1', $valueB);
    }
);

var_dump($taba);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.