两个时间关系对应的数组,怎么让它按时间顺序排列?

WBOY
Release: 2016-06-23 13:31:16
Original
1154 people have browsed it

两个时间关系对应的数组,其中一个是时间数组,怎么让它按时间顺序排列?
$x=Array("2015-07-06 22:49:00",
"2015-07-06 23:18:19",
"2015-07-06 18:30:12",
"2015-07-06 9:03:36",
"2015-07-05 18:31:52");
($y略)
array_multisort($x,SORT_DESC,SORT_STRING,$y);

print_r($x);
输出后是这样的:
Array ( [0] => 2015-07-06 9:03:36 [1] => 2015-07-06 23:18:19 [2] => 2015-07-06 22:49:00 [3] => 2015-07-06 18:30:12 [4] => 2015-07-05 18:31:52 )
应该是时间越晚越在前吧!可是后面的时间没有按先后顺序排列,是哪里有问题,盼解答


回复讨论(解决方案)

$x = Array( "2015-07-06 22:49:00", "2015-07-06 23:18:19", "2015-07-06 18:30:12", "2015-07-06 9:03:36", "2015-07-05 18:31:52"); foreach($x as $t) $r[] = strtotime($t);array_multisort($r, SORT_DESC, $x);print_r($x);
Copy after login
Array( [0] => 2015-07-06 23:18:19 [1] => 2015-07-06 22:49:00 [2] => 2015-07-06 18:30:12 [3] => 2015-07-06 9:03:36 [4] => 2015-07-05 18:31:52)
Copy after login

1、array_multisort 的最后一个参数是被排序的数组
前面的参数都是排序键和排序方式
你 array_multisort($x,SORT_DESC,SORT_STRING, $y);
是用 $x 对 $y 排序
2、你的 $x 的元素长度不一致,不宜使用 SORT_STRING

我把时间都改成 00:00:00 的格式就可以了,是吗?
我是用 $time=date('G:i:s'); 得到是时间,不能保证这个格式, date('G:i:s'); 这个要怎么写才能保持输出的是00:00:00 格式?谢谢版主

我把时间都改成 00:00:00 格式就可以了是吗?
请问 $time=date('G:i:s') 怎么改动才能总是输出想要的格式呢?谢谢版主解答

把时间改成 00::00:00 还不如不要时间只要日期

你只要把 2015-07-06 9:03:36
改成 2015-07-06 09:03:36
就可以用字符串比较
date('H:i:s')

时间我用的是date('H:i:s') 获取的,
12点以前输出的格式是前面少了个0,
怎么改date('H:i:s') 这个函数才能保证 输出的零不会省略掉,总是为00:00:00格式呢?
用什么输出方法我找不到告诉我一下

你在开玩笑?
H 24 小时格式,有前导零 00 到 23

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!