program tip

특정 시간대의 PHP에서 요일을 찾는 방법

radiobox 2020. 8. 31. 07:39
반응형

특정 시간대의 PHP에서 요일을 찾는 방법


날짜 / 시간을 처리하기 위해 PHP를 사용하는 동안 혼란 스럽습니다.

내가하려는 것은 이것이다 : 사용자가 내 페이지를 방문 할 때 나는 그의 시간대를 묻고 그의 시간대에 '요일'을 표시합니다.

브라우저의 날을 사용하고 싶지 않습니다. 이 계산을 PHP에서하고 싶습니다.

이것이 내가 그것을 달성하려는 방법입니다.

  1. 사용자가 입력 한 시간대
  2. php time () 함수로 계산 된 Unix 타임 스탬프.

하지만 어떻게 진행해야할지 모르겠습니다.이 시간대에서 '요일'을 어떻게 구할 수 있을까요?


$dw = date( "w", $timestamp);

$ dw는 0 (일요일)에서 6 (토요일)입니다. http://www.php.net/manual/en/function.date.php


내 해결책은 다음과 같습니다.

$tempDate = '2012-07-10';
echo date('l', strtotime( $tempDate));

출력은 다음과 같습니다. Tuesday

$tempDate = '2012-07-10';
echo date('D', strtotime( $tempDate));

출력은 다음과 같습니다. Tue


빠른 의견을 보내 주셔서 감사합니다.

이것이 제가 지금 사용할 것입니다. 누군가가 사용할 수 있도록 여기에 함수를 게시합니다.

public function getDayOfWeek($pTimezone)
{

    $userDateTimeZone = new DateTimeZone($pTimezone);
    $UserDateTime = new DateTime("now", $userDateTimeZone);

    $offsetSeconds = $UserDateTime->getOffset(); 
    //echo $offsetSeconds;

    return gmdate("l", time() + $offsetSeconds);

}

수정 사항이 있으면 신고하십시오.


나는 이것이 정답이라고 생각 Europe/Stockholm하며 사용자 시간대로 변경 하십시오.

$dateTime = new \DateTime(
    'now',
    new \DateTimeZone('Europe/Stockholm')
);
$day = $dateTime->format('N');

요일의 ISO-8601 숫자 표현 (PHP 5.1.0에 추가됨) 1 (월요일) ~ 7 (일요일)

http://php.net/manual/en/function.date.php

지원되는 시간대 목록은 http://php.net/manual/en/timezones.php를 참조 하십시오.


또 다른 빠른 방법 :

date_default_timezone_set($userTimezone);
echo date("l");

시간대 오프셋을 얻을 수있는 경우 현재 타임 스탬프에 추가 한 다음 gmdate 함수를 사용하여 현지 시간을 가져올 수 있습니다.

// let's say they're in the timezone GMT+10
$theirOffset = 10;  // $_GET['offset'] perhaps?
$offsetSeconds = $theirOffset * 3600;
echo gmdate("l", time() + $offsetSeconds);

$myTimezone = date_default_timezone_get();
date_default_timezone_set($userTimezone);
$userDay = date('l', $userTimestamp);
date_default_timezone_set($myTimezone);

This should work (didn't test it, so YMMV). It works by storing the script's current timezone, changing it to the one specified by the user, getting the day of the week from the date() function at the specified timestamp, and then setting the script's timezone back to what it was to begin with.

You might have some adventures with timezone identifiers, though.


"Day of Week" is actually something you can get directly from the php date() function with the format "l" or "N" respectively. Have a look at the manual

edit: Sorry I didn't read the posts of Kalium properly, he already explained that. My bad.


Check date is monday or sunday before get last monday or last sunday

 public function getWeek($date){
    $date_stamp = strtotime(date('Y-m-d', strtotime($date)));

     //check date is sunday or monday
    $stamp = date('l', $date_stamp);      
    $timestamp = strtotime($date);
    //start week
    if(date('D', $timestamp) == 'Mon'){            
        $week_start = $date;
    }else{
        $week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp));
    }
    //end week
    if($stamp == 'Sunday'){
        $week_end = $date;
    }else{
        $week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp));
    }        
    return array($week_start, $week_end);
}

Based on one of the other solutions with a flag to switch between weeks starting on Sunday or Monday

function getWeekForDate($date, $weekStartSunday = false){

    $timestamp = strtotime($date);

    // Week starts on Sunday
    if($weekStartSunday){
        $start = (date("D", $timestamp) == 'Sun') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Last Sunday', $timestamp));
        $end = (date("D", $timestamp) == 'Sat') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Next Saturday', $timestamp));
    } else { // Week starts on Monday
        $start = (date("D", $timestamp) == 'Mon') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Last Monday', $timestamp));
        $end = (date("D", $timestamp) == 'Sun') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Next Sunday', $timestamp));
    }

    return array('start' => $start, 'end' => $end);
}

echo date('l', strtotime('today'));

참고URL : https://stackoverflow.com/questions/712761/how-to-find-day-of-week-in-php-in-a-specific-timezone

반응형