PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Sat Sep 02, 2006 2:27 pm
I have an array of data with dates in an I want to sort it by date. I'm using:
Code: Select all
print_r($aEvents);
usort($aEvents, 'dateSort');
function dateSort($one, $two)
{
$one = strtotime($one['dateRange'][0]);
$two = strtotime($two['dateRange'][0]);
return $one - $two;
}I can see no reason why this doesn't work and yet I get
Code: Select all
<b>Warning</b>: usort() [<a href='http://uk.php.net/function.usort'>function.usort</a>]: Invalid comparison function
Here is the output from that print_r(), yes I know its already in order but I want to ensure it always will be. This array has been created from XML so I can't use ORDER BY.
Code: Select all
Array
(
[0] => Array
(
[dateRange] => Array
(
[0] => 2006-09-03
[1] => 2006-09-03
)
[name] => ITF England Squad training
)
[1] => Array
(
[dateRange] => Array
(
[0] => 2006-09-10
[1] => 2006-09-10
)
[name] => ITF England Squad training
)
[2] => Array
(
[dateRange] => Array
(
[0] => 2006-09-17
[1] => 2006-09-17
)
[name] => ITF England Squad training
)
[3] => Array
(
[dateRange] => Array
(
[0] => 2006-09-23
[1] => 2006-09-23
)
[name] => ETA Colour Belt seminar
)
[4] => Array
(
[dateRange] => Array
(
[0] => 2006-09-24
[1] => 2006-09-24
)
[name] => ETA Black belt grading & 2nd Kup and above seminar
)
<<snip>>
This...
Code: Select all
var_dump(dateSort($aEvents[0], $aEvents[1]));
var_dump(dateSort($aEvents[1], $aEvents[0]));...gives me...
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Sep 02, 2006 2:34 pm
I've tried your script snippet with the test data (please use var_export) and it works fine.
win xp, PHP 5.1.5 (cli) (built: Aug 15 2006 23:54:56)
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Sat Sep 02, 2006 3:03 pm
I'm using PHP Version 5.1.2
I couldn't find it in the bugs database but that doesn't mean its not there.
I put the data out of order and here's that var_export you wanted
Code: Select all
array (
0 =>
array (
'dateRange' =>
array (
0 => '2006-09-17',
1 => '2006-09-17',
),
'name' => 'ITF England Squad training',
),
1 =>
array (
'dateRange' =>
array (
0 => '2006-09-03',
1 => '2006-09-03',
),
'name' => 'ITF England Squad training',
),
2 =>
array (
'dateRange' =>
array (
0 => '2006-09-24',
1 => '2006-09-24',
),
'name' => 'ETA Black belt grading & 2nd Kup and above seminar',
),
3 =>
array (
'dateRange' =>
array (
0 => '2006-09-10',
1 => '2006-09-10',
),
'name' => 'ITF England Squad training',
),
4 =>
array (
'dateRange' =>
array (
0 => '2006-09-23',
1 => '2006-09-23',
),
'name' => 'ETA Colour Belt seminar',
),
)
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Sep 02, 2006 3:12 pm
Code: Select all
<?php
$aEvents = array (
0 =>
array (
'dateRange' =>
array (
0 => '2006-09-17',
1 => '2006-09-17',
),
'name' => 'ITF England Squad training',
),
1 =>
array (
'dateRange' =>
array (
0 => '2006-09-03',
1 => '2006-09-03',
),
'name' => 'ITF England Squad training',
),
2 =>
array (
'dateRange' =>
array (
0 => '2006-09-24',
1 => '2006-09-24',
),
'name' => 'ETA Black belt grading & 2nd Kup and above seminar',
),
3 =>
array (
'dateRange' =>
array (
0 => '2006-09-10',
1 => '2006-09-10',
),
'name' => 'ITF England Squad training',
),
4 =>
array (
'dateRange' =>
array (
0 => '2006-09-23',
1 => '2006-09-23',
),
'name' => 'ETA Colour Belt seminar',
)
);
usort($aEvents, 'dateSort');
print_r($aEvents);
function dateSort($one, $two)
{
$one = strtotime($one['dateRange'][0]);
$two = strtotime($two['dateRange'][0]);
return $one - $two;
}
?>output is
Code: Select all
Array
(
[0] => Array
(
[dateRange] => Array
(
[0] => 2006-09-03
[1] => 2006-09-03
)
[name] => ITF England Squad training
)
[1] => Array
(
[dateRange] => Array
(
[0] => 2006-09-10
[1] => 2006-09-10
)
[name] => ITF England Squad training
)
[2] => Array
(
[dateRange] => Array
(
[0] => 2006-09-17
[1] => 2006-09-17
)
[name] => ITF England Squad training
)
[3] => Array
(
[dateRange] => Array
(
[0] => 2006-09-23
[1] => 2006-09-23
)
[name] => ETA Colour Belt seminar
)
[4] => Array
(
[dateRange] => Array
(
[0] => 2006-09-24
[1] => 2006-09-24
)
[name] => ETA Black belt grading & 2nd Kup and above seminar
)
)no error, no warning.
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Sat Sep 02, 2006 3:16 pm
OK thanks volka.
I think I can safely assume when I deploy this on the production server (with a newer version of PHP) its going to work.
You know this isn't the first bug like this I've found today.
Code: Select all
if ($tTkd->local) {
// this is just a hack to get it to work locally.
$withoutExt = substr($file, 0, -4);
// because my version of PHP doesn't return filename element from pathinfo
} else {
$withoutExt = pathinfo($file);
$withoutExt = $withoutExt['filename'];
}
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Sep 02, 2006 3:32 pm
ole wrote: I think I can safely assume when I deploy this on the production server (with a newer version of PHP) its going to work.
I wouldn't