Check how many dates are equal

Asked

Viewed 67 times

0

I need a help to know how many equal dates for PHP to present me with a number count, for example, "3 dates are equal", what I have of code so far is this:

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

}
  • How many dates are equal??? you have one array??? or only this code?

  • I’m taking it from the database and comparing it to the date(Y-m-d) of php

  • if it doesn’t make a group by?

  • You can take this direct count from the bank, it is more practical and will probably have greater performance. Making a query that returns the number of equal dates

  • I’ll try, any doubt I’ll come back here.

2 answers

2


You can do this in two ways:

Assuming you’re 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;
    }
}
  • In the case that is inside the Count would be the table column and the last comparative date?

  • both would be the name of the column with the date

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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.