Check how many dates are the same

0

I need a help to know how many equal dates for PHP to give me a count in numbers, for example "3 dates are the same", what I have to code so far is this:

$hoje = '2018-07-02';
$datafinal = '2018-07-02';
if(strtotime($hoje) == strtotime($datafinal){

}
    
asked by anonymous 02.07.2018 / 22:58

2 answers

2

You can do this in two ways:

Assuming you are making a selection at the bank:

SELECT count(data) as count_data GROUP BY data

Or if you want to do with PHP:

$array_datas = [
    '2018-06-10',
    '2018-06-12',
    '2018-06-10',
    '2018-06-10'
];
$hoje = date('Y-m-d');
$iguais = 0;
foreach($array_datas as $data){
    if($data == $hoje){
        $iguais += 1;
    }
}
    
03.07.2018 / 00:28
0

Prefer to use the PHP DateTime object.

<?php
$hoje   =  
DateTime::createFromFormat('Y-m-d',date('Y-m-d'));
$datafinal = 
DateTime::createFromFormat('Y-m-d','2018-07-05');
$datasIguais =0;
// $datasIguais = $hoje == $datafinal ? $datasIguais+1 : $datasIguais;
if($hoje == $datafinal){
  $datasIguais++;
}
echo $datasIguais;
    
06.07.2018 / 01:25