Reputation: 422
I'm trying to delete array elements older than 1 month.
All array elements contains a date value which I'm trying to compare in PHP using strtotime
+1 month.
The array looks like this:
[
{
"userid": "f2eca630-f89a-4b7f-b463-d834a2b50a24",
"name": "4q9fenYDD3Czy2fyTkQmIlvj-1590978189-9BBNrQE5.webp",
"gender": "female",
"rating": 61231,
"date": "2020-04-08 02:23:09",
"description": ""
},
{
"userid": "f2eca630-f89a-4b7f-b463-d834a2b50a24",
"name": "o0SuDVEytVBIMJkveNWamamF-1590990218-27UmvlNd.webp",
"gender": "male",
"rating": 0,
"date": "2020-06-01 03:41:38",
"description": ""
}
]
I'm checking the array with the following PHP code:
$datenow = date("Y-m-d H:i:s");
$expireDate = date("Y-m-d H:i:s",strtotime('+1 month',strtotime($datenow)));
foreach ($monthlyArray as $key => $item) {
$gotDate = $item['date'];
echo "Comparing Array Date: ($gotDate) with Current Date +1 month: ($expireDate) <br>";
if ($gotDate > $expireDate) {
echo $gotDate . " will be removed because of: " . $expireDate ."<br>";
//unset ($monthlyArray[$key]);
}
}
Note that $monthlyArray contains the array above.
The results I get when I run this code:
Comparing Array Date: (2020-04-08 02:23:09) with Current Date +1 month: (2020-06-01 09:19:09)
Comparing Array Date: (2020-06-01 03:41:38) with Current Date +1 month: (2020-06-01 09:19:09)
As you can see, the if ($gotDate > $expireDate)
didn't get fired which is very odd because the first element in the array has a date set to 2020-04-08 02:23:09 which is indeed more than 1 month old.
What am I doing wrong?
Upvotes: 0
Views: 424
Reputation: 147196
I think you have your logic the wrong way round; you should be comparing the datetime in $item
plus one month with the current datetime, and if it's earlier than that, the item has expired and should be deleted. You can use array_filter
to remove the expired items from the array:
$monthlyArray = array_filter($monthlyArray, function ($item) {
$expiry = (new DateTime($item['date']))->modify('+1 month');
$now = new DateTime();
return $expiry >= $now;
});
print_r($monthlyArray);
Output:
Array
(
[1] => Array
(
[userid] => f2eca630-f89a-4b7f-b463-d834a2b50a24
[name] => o0SuDVEytVBIMJkveNWamamF-1590990218-27UmvlNd.webp
[gender] => male
[rating] => 0
[date] => 2020-06-01 03:41:38
[description] =>
)
)
If performance is an issue, pre-computing $now
and using strtotime
will be more efficient:
$now = time();
$monthlyArray = array_filter($monthlyArray, function ($item) use ($now) {
$expiry = strtotime('+1 month', strtotime($item['date']));
return $expiry >= $now;
});
and a simple foreach
with unset
is probably faster still:
$now = time();
foreach ($monthlyArray as $key => $item) {
$expiry = strtotime('+1 month', strtotime($item['date']));
if ($expiry < $now) unset($monthlyArray[$key]);
}
Upvotes: 2
Reputation: 684
Simply compare the Dates
// $datenow = date("Y-m-d H:i:s");
$expireDate = date("Y-m-d H:i:s",strtotime('-1 month'));
foreach ($monthlyArray as $key => $item) {
$gotDate = $item['date'];
echo "Comparing Array Date: ($gotDate) with Current Date +1 month: ($expireDate) <br>";
if ($gotDate < $expireDate) {
echo $gotDate . " will be removed because of: " . $expireDate ."<br>";
//unset ($monthlyArray[$key]);
}
}
Upvotes: 0
Reputation: 1105
Try comparing timestamps instead of date
objects :
$datenow = date("Y-m-d H:i:s");
$expireDateTimestamp = strtotime('+1 month',strtotime($datenow));
$expireDate = date("Y-m-d H:i:s", $expireDateTimestamp);
foreach ($monthlyArray as $key => $item) {
$gotDate = $item['date'];
$gotDateTimestamp = strotime($gotDate);
echo "Comparing Array Date: ($gotDate) with Current Date +1 month: ($expireDate) <br>";
if ($gotDateTimestamp > $expireDateTimestamp) {
echo $gotDate . " will be removed because of: " . $expireDate ."<br>";
//unset ($monthlyArray[$key]);
}
}
Upvotes: 0