简体   繁体   中英

How to create today, tomorrow and yesterday with MySQL

first of all I have searched the forum for getting an answer to my problem. I am creating a weekly schedule with PHP & MySQL and the problem is that I can not figure out how to setup the case statement when the actual day is Sunday and I need to set Monday as Tomorrow. Here is my simple MySQL table week

Fields: dayid(int,primary,auto-increment),day(varchar(25))
Data: (1,'Monday'),(2,'Tuesday'),(3,'Wednesday'),(4,'Thursday'),
      (5,'Friday'),(6,'Saturday'),(7,'Sunday');

Here is my php code:

$today = date("N");
$query = "SELECT *,case when dayid = '$today' THEN 'Today'
                        when dayid = '$today' + 1 THEN 'Tomorrow'
                        when dayid = '$today' - 1 THEN 'Yesterday'
                        ELSE day END days FROM week ";
$results = mysqli_query($link, $query);
while ($r = mysqli_fetch_array($results)) {
echo '<p>'.$r['days'].'</p>'; }

Like I wrote before I can not figure out how to set it up so when $today = 7 (Sunday) that Monday will say Tomorrow and the other way around so when it´s Monday that it says yesterday instead of Sunday. Hopefully someone can help me solve this.

Thank you in advance!

You need to use IF to wrap around at the end of the week:

CASE dayid
    WHEN $today THEN Today
    WHEN IF($today = 7, 1, $today + 1) THEN 'Tomorrow'
    WHEN IF($today = 1, 7, $today - 1) THEN 'Yesterday'
    ELSE day
END days

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