Always use date
fields in your table, not varchar
fields with data of type date
, may cause future problems. Another factor does not use mysql_*
it is deprecated in the new versions of PHP . Even though I built 3 examples: mysql
, mysqli
and pdo
see below:
Example:
1) Mysql (not recommended)
<?php
function GetSemana($date){
list($d,$m,$y) = explode('.', $date);
switch (strtolower(date('l', mktime(0, 0, 0, $m ,$d,$y))))
{
case 'sunday': {
return ' no domingo';
break;
}
case 'monday': {
return ' na segunda-feira';
break;
}
case 'tuesday': {
return ' na terça-feira';
break;
}
case 'wednesday': {
return ' na quarta-feira';
break;
}
case 'thursday': {
return ' na quinta-feira';
break;
}
case 'friday': {
return ' na sexta-feira';
break;
}
case 'saturday': {
return ' no sábado';
break;
}
}
}
$conn = mysql_connect("localhost", "root", "senha");
mysql_select_db("generics", $conn);
$query = mysql_query("SELECT user_id, data, count(user_id) as datacount FROM entregas GROUP BY user_id, data", $conn);
while($item = mysql_fetch_array($query))
{
echo '['.$item['data'].'] ';
echo ' O usuario '.$item['user_id'];
echo ' possui '.$item['datacount'];
echo ' data'.((int)$item['datacount']>1?'s':'');
echo GetSemana($item['databr']);
echo '<br>';
}
2) Mysqli
<?php
function GetSemana($date){
list($d,$m,$y) = explode('.', $date);
switch (strtolower(date('l', mktime(0, 0, 0, $m ,$d,$y))))
{
case 'sunday': {
return ' no domingo';
break;
}
case 'monday': {
return ' na segunda-feira';
break;
}
case 'tuesday': {
return ' na terça-feira';
break;
}
case 'wednesday': {
return ' na quarta-feira';
break;
}
case 'thursday': {
return ' na quinta-feira';
break;
}
case 'friday': {
return ' na sexta-feira';
break;
}
case 'saturday': {
return ' no sábado';
break;
}
}
}
$conn = mysqli_connect("localhost", "root", "senha", "generics");
$query = mysqli_query($conn, "SELECT user_id, data, count(user_id) as datacount FROM entregas GROUP BY user_id, data");
while($item = mysqli_fetch_array($query))
{
echo '['.$item['data'].'] ';
echo ' O usuario '.$item['user_id'];
echo ' possui '.$item['datacount'];
echo ' data'.((int)$item['datacount']>1?'s':'');
echo GetSemana($item['databr']);
echo '<br>';
}
3) PDO
<?php
function GetSemana($date){
list($d,$m,$y) = explode('.', $date);
switch (strtolower(date('l', mktime(0, 0, 0, $m ,$d,$y))))
{
case 'sunday': {
return ' no domingo';
break;
}
case 'monday': {
return ' na segunda-feira';
break;
}
case 'tuesday': {
return ' na terça-feira';
break;
}
case 'wednesday': {
return ' na quarta-feira';
break;
}
case 'thursday': {
return ' na quinta-feira';
break;
}
case 'friday': {
return ' na sexta-feira';
break;
}
case 'saturday': {
return ' no sábado';
break;
}
}
}
$pdo = new PDO("mysql:host=localhost;dbname=generics", "root", "senha");
$res = $pdo->query("SELECT user_id, data, count(user_id) as datacount FROM entregas GROUP BY user_id, data");
foreach($res as $item){
echo '['.$item['data'].'] ';
echo ' O usuario '.$item['user_id'];
echo ' possui '.$item['datacount'];
echo ' data'.((int)$item['datacount']>1?'s':'');
echo GetSemana($item['databr']);
echo '<br>';
}
Obs: can adapt to your script.