매 주 월요일 구하기
본문
매 주 월요일과 월요일까지 남은 시간을 구현해서 출력해야 하는데요.
만약 오늘 날짜(10/6) 기준으로 다음 월요일은 (11/9)이고 11월 9일까지 남은 시간은 몇일 몇시간 몇분 입니다.
만약 월요일이 당일이면 오늘은 월요일 입니다. 라는 문구가 나와야 하는데 이 것을 코드로 짤 수 있을까요?ㅠㅠ
답변 4
<?php
$daytime = 60*60*24; //86400
$offset = $daytime * 4 - 60*60*9; //1970-01-01 09:00:00(목요일 9시가 0)
function getNthMondaytime($n){
global $daytime;
global $offset;
return ($daytime * $n) * 7 + $offset;
}
function getTimeStr($time){
global $daytime;
$seperator = "";
$sec = $time%60;
$time = floor($time/60);
$minute = $time%60;
$time = floor($time/60);
$hour = $time%24;
$time = floor($time/24);
$day = $time;
$str = "";
if($day)
{
$str .= $day . '일';
$seperator = " ";
}
// if($hour)
// {
$str .= $seperator . str_pad($hour, 2, "0", STR_PAD_LEFT) . '시간';
$seperator = " ";
// }
// if($minute)
// {
$str .= $seperator . str_pad($minute, 2, "0", STR_PAD_LEFT) . '분';
$seperator = " ";
// }
// if($sec)
// {
//초까지 필요한 경우
// $str .= $seperator . str_pad($sec, 2, "0", STR_PAD_LEFT) . '초';
// $seperator = " ";
// }
return $str;
}
$time = time() - $offset;
$current_week = floor(floor($time/$daytime)/7);
$current_weekday = floor($time/$daytime)%7;
echo $current_week . "/" . $current_weekday . "<br>";
if($current_weekday == 0)
{
echo "오늘은 월요일입니다.";
}
else
{
$next_week_time = getNthMondaytime($current_week+1);
echo getTimeStr($next_week_time - time()) . ' 남았습니다.';
}
https://sir.kr/qa/461528?#answer_461550
여기에서
if (today.getDay() % 6 == 0) 을
if (today.getDay() == 1) 로 바꾸면 월요일을 표현할 수 있습니다.
----------
https://ahnsisters.tistory.com/27 를 참고하시면 다음주 월요일을 구할 수 있습니다.
----------
위 두가지를 바탕으로 남은시간을 카운트하면 되겠습니다.
친절한 GPT
$today = time(); // 현재 시간을 가져옵니다.
$nextMonday = strtotime('next Monday', $today); // 다음 주 월요일을 계산합니다.
// 다음 주 월요일까지의 남은 시간을 계산합니다.
$remainingTimeInSeconds = $nextMonday - $today;
// 남은 시간을 일, 시간, 분으로 분해합니다.
$daysRemaining = floor($remainingTimeInSeconds / (60 * 60 * 24));
$hoursRemaining = floor(($remainingTimeInSeconds % (60 * 60 * 24)) / (60 * 60));
$minutesRemaining = floor(($remainingTimeInSeconds % (60 * 60)) / 60);
echo "다음 주 월요일까지 남은 기간: {$daysRemaining}일 {$hoursRemaining}시간 {$minutesRemaining}분";
!-->
GTP에게 먼저 물어보세요
<?php
// 현재 날짜를 가져옵니다.
$currentDate = new DateTime();
// 다음 주 월요일을 계산합니다.
$nextMonday = clone $currentDate;
$nextMonday->modify('next Monday');
// 남은 시간을 계산합니다.
$interval = $currentDate->diff($nextMonday);
// 결과를 출력합니다.
echo "현재 날짜: " . $currentDate->format('Y-m-d') . "<br>";
echo "다음 주 월요일: " . $nextMonday->format('Y-m-d') . "<br>";
echo "남은 일수: " . $interval->format('%a') . "일<br>";
echo "남은 시간: " . $interval->format('%h') . "시간 " . $interval->format('%i') . "분<br>";
?>
https://chat.openai.com/c/223aae83-bbaf-44ee-b0c5-8e7a594acd20
!-->
답변을 작성하시기 전에 로그인 해주세요.