Saturday 20 January 2018

Sorting time array giving wrong values in PHP

I have a php array like
this:-


array(3) {

[0]=>
string(4) "9:30"
[1]=>
string(5)
"15:00"
[2]=>
string(5)
"13:00"
}

After sorting
this time i am getting wrong data. i am sorting it by
array_multisort function.


This is
what i am getting after time is
sorted


array(3) {

[0]=>
string(5) "03:00"
[1]=>
string(5)
"01:00"
[2]=>
string(5) "09:30"

}

This is my
code


first i am converting my time to unix time by using
strtotime function then sorting like
this:


while($row11 =
mysqli_fetch_array($hourget))
{
$totlahourdat1[] =
strtotime($row11['hours']);
$totlahourdat2[] = $row11['hours'];

}
echo "
";
var_dump($totlahourdat2);
echo
"
";
//sort data
array_multisort($totlahourdat1,
SORT_DESC);
//var_dump($totlahourdat);
foreach($totlahourdat1 as
$time){
$totlahourdat[] = date("h:i",$time);
}
echo
"
";
var_dump($totlahourdat);
echo
"
";

if i print my
$totlahourdat1 array then i get
this:


array(3) {

[0]=>
int(1500535800)
[1]=>

int(1500555600)
[2]=>
int(1500548400)

}

My result should be
like:


array(3) {

[0]=>
string(4) "9:30"
[1]=>
string(5)
"13:00"
[2]=>
string(5)
"15:00"
}

Any help will
be highly appreciated.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...