DEV Community

andysaktia
andysaktia

Posted on • Edited on

Update Default Date Display

This time I will process the data that appears on the page with special conditions, where when today is Saturday or Sunday, the date will display the previous date, namely Friday.

Stages:

  1. Today's date, time form and day number (w)
  2. Giving the day deduction parameter ($minus)
  3. Update the date to be displayed ($t)
    $today= date('Y-m-d'); // bisa diganti dengan GETtanggal 
    $time = strtotime($today);
    $day_num = date('w', $time);

    if ($day_num == '0') {
      $minus = '-2 day';
    } elseif ($day_num == '6') {
      $minus = '-1 day';
    } else {
      $minus = '0 day';
    }

    $t = strtotime($today . $minus);
    $t = date('Y-m-d', $t);
Enter fullscreen mode Exit fullscreen mode

or

  // case date adalah hari ini
    $day_num = date('w'); 

    if ($day_num == '0') {
      $minus = '-2 day';
    } elseif ($day_num == '6') {
      $minus = '-1 day';
    } else {
      $minus = '0 day';
    }

    $t = date('Y-m-d', strtotime($minus));
Enter fullscreen mode Exit fullscreen mode

DONE

Top comments (0)