-2

我需要重写数组中每个酒店房间可用的日期范围。这是我的数组值:

1=2012-10-07
1=2012-10-08
1=2012-10-09
1=2012-10-10
2=2012-10-07
2=2012-10-08
2=2012-10-09
2=2012-10-10
3=2012-10-05
3=2012-10-06
3=2012-10-08
3=2012-10-09
3=2012-10-10

我需要把它写在:

1 available from 7-10 October 2012 //I know this line need to use date("F Y",strtotime());
2 available from 7-10 October 2012 
3 available from 5-10 October 2012

请建议如何将这些天栈写入 PHP 中的日期范围。问候,

4

3 回答 3

3

像这样的东西应该工作:

$dates = array('2000-10-02', '2000-10-01', '2000-10-03');
sort($dates);

// prints "1-3 October 2000"
echo date('j', strtotime(current($dates))) . '-'
   . date('j', strtotime(end($dates))) . ' '
   . date('F Y', strtotime(current($dates)));

改进领域:

  • 当日期范围不连续时会发生什么?
  • 当一个范围跨越两个不同的年份或月份时会发生什么?
于 2012-10-02T04:15:37.867 回答
2

你去http://codepad.viper-7.com/EKo9ky

$dates = array(
    array(1, "2012-10-07"),
    array(1, "2012-10-08"),
    array(1, "2012-10-09"),
    array(1, "2012-10-10"),
    array(2, "2012-10-07"),
    array(2, "2012-10-08"),
    array(2, "2012-10-09"),
    array(2, "2012-10-10"),
    array(3, "2012-10-05"),
    array(3, "2012-10-06"),
    array(3, "2012-10-08"),
    array(3, "2012-10-09"),
    array(3, "2012-10-10")
);
$info = array();
foreach($dates as $roominfo) {
    $time = strtotime($roominfo[1]);
    $room = $roominfo[0];
    if(!array_key_exists($roominfo[0], $info))
        $info[$room] = array($time, $time);
    if($time<$info[$room][0])
        $info[$room][0] = $time;
    if($time>$info[$room][1])
        $info[$room][1] = $time;
}

foreach($info as $roomId => $roomRange) {
    echo $roomId.' available from '.
        date('d', $roomRange[0]).' - '.
        date('d F Y', $roomRange[1]).' <br/>';
}

输出:

1 available from 07 - 10 October 2012
2 available from 07 - 10 October 2012
3 available from 05 - 10 October 2012

这并不完全符合您的要求,但我认为这应该足以达到您的目标。小心,房间不能在某个范围内一天可用,请记住这一点。

于 2012-10-02T04:13:11.013 回答
1

以下是有关如何完成任务的一些流程。

首先根据您的 1,2,3 将它们切片

那么你可以像这样处理你的日期:

不使用排序,当前,结束。我们可以只使用最小和最大数组;

<?php
$dates = array('2012-10-07','2012-10-10','2012-10-01');
$stimes = array_map('strtotime',$dates);

var_dump($dates);
var_dump($stimes);

echo date('F j, Y', min($stimes));
echo "<br />\n";
echo date('F j, Y', max($stimes));
于 2012-10-02T04:25:34.743 回答