Assuming I have multiple timestamps of multiple years and months in an array of timestamps (eg 123456, 111222333, 2112321321, 232234422, 1111343222, 231111211, etc), I want to sort them into this format:
2012
06
09
2004
03
08
2003
01
02
Is there any way of doing this? I am using PHP
I've found a solution:
foreach($timestamps as $time){
$year = date('Y', $time);
$month = date('m', $time);
if(!$ts_arr[$year]){
$ts_arr[$year] = array();
}
array_push($ts_arr[$year], $month);
$ts_arr[$year] = array_unique($ts_arr[$year]);
}
It's not perfect, but it works!
You can do this.
See if this gets you started; it's incomplete but takes care of most of it. Just a few additions and it should work how you want.
<?php
$timestamps = array();
for ($i = 0; $i < 100; ++$i) {
$ts = mktime(rand(0, 23), rand(0, 59), rand(0, 59), rand(1, 12), rand(1, 31), rand(2000, 2012));
$timestamps[] = $ts;
}
sort($timestamps);
$lastYear = null;
echo "<ul>
";
foreach($timestamps as $ts) {
$year = date('Y', $ts);
if ($year != $lastYear) {
if ($lastYear != null) {
echo "</ul>
";
}
$lastYear = $year;
echo "<li>$year<ul>
";
}
echo "<li>" . date('m-d', $ts) . "</li>
";
}
echo "</ul></ul>";
I create an array of 100 random timestamps to populate the list at the beginning.