【问题标题】:Check if multiple datetime are same in php foreach检查php foreach中的多个日期时间是否相同
【发布时间】:2018-08-17 16:51:12
【问题描述】:

我想拆分具有相同日期和不同日期的项目。

foreach ($cart->getAllItems() as $item)
{
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime(); 
$pickupDateTime = date('Y-m-d G:i:s', strtotime($pickupDateTime)) //2018-03-09 6:03:00 or 2018-03-09 21:10:00

// Split items here with same Date //

if(Dates are Same Condition){
$items_normal[]= $item->getProductId();
}else
{
//If Dates Are diffrent
$ites_special[]= $item->getProductId();
}

}

需要比较第一个if条件中的日期是否相同,否则需要比较日期不同的项目。

看下面的数组,在这个我有相同的日期和时间,我需要去俱乐部那些有相同的日期和时间。在下面的例子中,第三个必须在第一个数组下,因为它有不同的时间,即 6:05 而不是 6:03

    Array
(
    [1] => Array
        (
            [product_id] => 742
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:03 AM
            [tax] => 0
            [splinst] => null
        )

    [2] => Array
        (
            [product_id] => 743
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:03 AM
            [tax] => 0
            [splinst] => null
        )

)

Array
(
    [3] => Array
        (
            [product_id] => 744
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:03 AM
            [tax] => 0.12
            [splinst] => null
        )

    [4] => Array
        (
            [product_id] => 757
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:05 AM
            [tax] => 0.25
            [splinst] => null
        )

)

【问题讨论】:

  • 为此,您可以将 daets 保存在数组中并使用 is_array() 来检查同一日期
  • 我们如何检查 in_array 是否有相同的日期对象?
  • 你只有一个相同的日期吗?
  • 不,多个日期时间可以相同

标签: php arrays sorting datetime php-5.6


【解决方案1】:

你应该使用 DateTime 对象:

$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime(); 
$pickupDateTime = new DateTime($pickupDateTime);

你可以比较两个 DateTime 对象:

if ($dateTimeObject1 == $dateTimeObject2) {
    // both have same date and time
}

请注意,您不应在此处使用严格比较 (===),因为它会检查它是否是同一个对象,而不是 DateTime 是否相同。

如果您只需要比较日期而忽略时间,您可以在两个对象上调用 setTime 将时间重置为 0:

$dateTimeObject1->setTime(0, 0, 0);
$dateTimeObject2->setTime(0, 0, 0);
if ($dateTimeObject1 == $dateTimeObject2) {
    // both have the same date
}

【讨论】:

  • 需要比较完整的日期时间对象
【解决方案2】:

您可以将键中的日期存储到数组中以对项目进行分组:

foreach ($cart->getAllItems() as $item)
{
    $pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime(); 
    $pickupDateTime = date('Y-m-d G:i:s', strtotime($pickupDateTime)) //2018-03-09 6:03:00 or 2018-03-09 21:10:00
    $items_special[$pickupDateTime][] = $item->getProductId();
}
foreach ($items_special as $date => $items) {
    if (count($items) > 1) {
        $items_normal = $items ;
        unset($items_special[$date]);
        break;
    }
}
$items_special = array_values($items_special);

所以,$items_special 看起来像:

Array
(
    [0] => Array
        (
            [product_id] => 757
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:05 AM
            [tax] => 0.25
            [splinst] => null
        )

)

$items_normal 看起来像:

Array
(
    [0] => Array
        (
            [product_id] => 742
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:03 AM
            [tax] => 0
            [splinst] => null
        )

    [1] => Array
        (
            [product_id] => 743
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:03 AM
            [tax] => 0
            [splinst] => null
        )

    [2] => Array
        (
            [product_id] => 744
            [qty] => 1
            [date] => 03/09/2018
            [time] => 06:03 AM
            [tax] => 0.12
            [splinst] => null
        )
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多