-1

How can I get the week number of a date by using mysqli for each entry?

Given I have table1 containing a date column with the following two entries:

2018-03-14
2018-05-14

I wish to see the week numbers of the above two entries. My main problem is that I do not know how to process the result of my query.


$sql = 'SELECT WEEK(date) FROM table1 WHERE YEAR(beginn) = "2018"';

$db_erg = mysqli_query($con, $sql);

while ($row = $db_erg->fetch_array())
{
  echo $row[0]; // or $row['week'] if you used AS week
}
user9554121
  • 17
  • 1
  • 7

1 Answers1

1
$sql = 'SELECT WEEK(date) AS week FROM table1 WHERE YEAR(date) = 2018';

For the WHERE clause, you might want to investigate whether avoiding the YEAR method provides performance advantages.

WHERE date BETWEEN CAST('2018-01-01' AS DATE) AND CAST('2018-12-31' AS DATE);

WHERE date >= CAST('2018-01-01' AS DATE) AND date <= CAST('2018-12-31' AS DATE);

$db_erg in the above is the results (a mysqi_result object), so you should be able to get them with a while loop.

// check query succeeded
if ($db_erg) {

    while ($row = $db_erg->fetch_array())
    {
        // process the data
        echo $row[0]; // or $row['week'] if you used AS week
    }
}
vogomatix
  • 4,856
  • 2
  • 23
  • 46