테스트 사이트 - 개발 중인 베타 버전입니다

매 주 월요일 구하기 채택완료

015B 2년 전 조회 2,442

매 주 월요일과 월요일까지 남은 시간을 구현해서 출력해야 하는데요.

 

만약 오늘 날짜(10/6) 기준으로 다음 월요일은 (11/9)이고 11월 9일까지 남은 시간은 몇일 몇시간 몇분 입니다.

만약 월요일이 당일이면 오늘은 월요일 입니다. 라는 문구가 나와야 하는데 이 것을 코드로 짤 수 있을까요?ㅠㅠ

댓글을 작성하려면 로그인이 필요합니다.

답변 4개

채택된 답변
+20 포인트
지아부
2년 전

</p>

<p> </p>

<p><?php </p>

<p>$daytime = 60*60*24; //86400

$offset =  $daytime * 4 - 60*60*9; //1970-01-01 09:00:00(목요일 9시가 0)</p>

<p> </p>

<p>function getNthMondaytime($n){

    global $daytime;

    global $offset;

    

    return ($daytime * $n) * 7 + $offset;

}</p>

<p>

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;

    

}</p>

<p>$time = time() - $offset;</p>

<p>$current_week = floor(floor($time/$daytime)/7);

$current_weekday = floor($time/$daytime)%7;</p>

<p>echo $current_week . "/" . $current_weekday . "
";</p>

<p>if($current_weekday == 0)

{

    echo "오늘은 월요일입니다.";

}

else

{

    $next_week_time = getNthMondaytime($current_week+1);

    

    echo getTimeStr($next_week_time - time()) . ' 남았습니다.';

}</p>

<p> </p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

0
015B
2년 전
감사합니다.

댓글을 작성하려면 로그인이 필요합니다.

2년 전

GTP에게 먼저 물어보세요

 

</p>

<p><?php

// 현재 날짜를 가져옵니다.

$currentDate = new DateTime();</p>

<p>// 다음 주 월요일을 계산합니다.

$nextMonday = clone $currentDate;

$nextMonday->modify('next Monday');</p>

<p>// 남은 시간을 계산합니다.

$interval = $currentDate->diff($nextMonday);</p>

<p>// 결과를 출력합니다.

echo "현재 날짜: " . $currentDate->format('Y-m-d') . "
";

echo "다음 주 월요일: " . $nextMonday->format('Y-m-d') . "
";

echo "남은 일수: " . $interval->format('%a') . "일
";

echo "남은 시간: " . $interval->format('%h') . "시간 " . $interval->format('%i') . "분
";

?>

 

https://chat.openai.com/c/223aae83-bbaf-44ee-b0c5-8e7a594acd20

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

0
015B
2년 전
감사합니다 다음부턴 챗 GPT를 사용해봐야겠ㄴ예요^^

댓글을 작성하려면 로그인이 필요합니다.

친절한 GPT 

 

</p>

<p>$today = time(); // 현재 시간을 가져옵니다.

$nextMonday = strtotime('next Monday', $today); // 다음 주 월요일을 계산합니다.</p>

<p>// 다음 주 월요일까지의 남은 시간을 계산합니다.

$remainingTimeInSeconds = $nextMonday - $today;</p>

<p>// 남은 시간을 일, 시간, 분으로 분해합니다.

$daysRemaining = floor($remainingTimeInSeconds / (60 * 60 * 24));

$hoursRemaining = floor(($remainingTimeInSeconds % (60 * 60 * 24)) / (60 * 60));

$minutesRemaining = floor(($remainingTimeInSeconds % (60 * 60)) / 60);</p>

<p>echo "다음 주 월요일까지 남은 기간: {$daysRemaining}일 {$hoursRemaining}시간 {$minutesRemaining}분";

 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

0
015B
2년 전
감사합니다

댓글을 작성하려면 로그인이 필요합니다.

https://sir.kr/qa/461528?#answer_461550

여기에서

if (today.getDay() % 6 == 0) 을

if (today.getDay() == 1) 로 바꾸면 월요일을 표현할 수 있습니다.

 

----------

 

https://ahnsisters.tistory.com/27 를 참고하시면 다음주 월요일을 구할 수 있습니다.

 

----------

 

https://sir.kr/g5_tip/18467

위 두가지를 바탕으로 남은시간을 카운트하면 되겠습니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

0
015B
2년 전
감사합니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인