简体   繁体   中英

Getting day of the week (mon, tues, wed) based on Y:m:d format saved in database field

I have a database table called 'transactions' with the field 'actualdate' that has the date of each row saved like 2014-09-13 .

I'm displaying the results using this query:

"SELECT id,actualdate, SUM(points) FROM transactions GROUP BY DAY(actualdate) ORDER BY id DESC limit 7"

Then displaying the results with:

while($row = mysqli_fetch_array($result) ) {
    echo "<li>";
    echo $row['actualdate'];
    echo " ";
    echo $row['SUM(points)'];
    echo "</li>";
}

Is it possible to echo what day of the week it was for each summed result based on the date the way it's formatted?

Thanks.

Yes of course, you can use Datetime in this case:

$row['actualdate'] = '2014-09-13';
$date = new DateTime($row['actualdate']);
echo $date->format('l'); // l represents Monday, Tuesday,
// in this case "Saturday"

// For three letter representations use this:
echo $date->format('D'); // Sat

Applying it:

while($row = mysqli_fetch_assoc($result) ) {
    echo "<li>";
    $date = new DateTime($row['actualdate']);
    echo $date->format('D');
    echo " ";
    echo $row['SUM(points)'];
    echo "</li>";
}

如果只有那一天

$day=date( 'l', strtotime($row['actualdate']) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM