Always use fields date
on your table, not fields varchar
with data of the type date
, can bring future problems. Another factor does not use mysql_*
he is depreciated in the new versions of PHP. Even so 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.
Each BD has its functions to manipulate dates, look for the BD documentation in question for details. But in any case try ANSI EXTRACT http://users.atw.hu/sqlnut/sqlnut2-chp-4-sect-4.html
– Motta
What exit do you get like this?
– Jorge B.
Is the date in the database the one mentioned above?? DD/MM/YYYY?? or ta registered with the YYYY/MM/DD database standard, because if you have it in the default format, I send you a function that it already returns everything straight from the bank
– Douglas Bernardino