Php haftanın gününü bulmak için nasıl

8 Cevap php

Ben tarih / saat işlemek için php kullanırken karıştı.

Ne yapmaya çalışıyorum şudur: Bir kullanıcı sayfamı ziyaret ettiğinde ben onun dilimini soran ve sonra onun bir zaman diliminde 'Haftanın bir gününü' görüntüleniyor duyuyorum.

I don't want to use the browser's day. I want to do this calculation in php.

Bu ben bunu başarmak için çalışıyorum nasıl:

  1. Kullanıcı tarafından girilen timezone
  2. Php zaman () işlevi tarafından hesaplanan Unix zaman damgası.

But I dont know how to proceed... How would i get the 'day of week' in this timezone.

8 Cevap

Benim çözüm şudur:

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

Output: Tuesday

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

Output: Tue

Thanks a lot pratik yorumlar için çocuklar.

This is what i will be using now. Posting the function here so that somebody may use it.

public function getDayOfWeek($pTimezone)
{

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

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

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

}

Herhangi bir düzeltme bulursanız bildirin.

Onların timezone offset alabilirsiniz, sadece mevcut zaman damgası eklemek ve daha sonra kendi yerel saati almak için gmdate işlevini kullanabilirsiniz.

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

Başka bir hızlı yolu:

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

Bu (test, yani YMMV vermedi) çalışması gerekir. Bu betiğin geçerli zaman dilimini depolayarak kullanıcı tarafından belirtilen birine değişen, belirtilen zaman damgası ile date() işlevinden haftanın gün oluyor, ve sonra tekrar ne için betiğin dilimini ayarlayarak çalışır ile başlamak oldu.

Eğer olsa, timezone künyeleri ile bazı maceralar olabilir.

"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: düzgün Kalium arasında mesajları okumadım Maalesef, o zaten açıkladı. Benim kötü.

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);
}

Pazar veya Pazartesi günü başlayan hafta arasında geçiş yapmak için bir bayrak ile diğer çözümlerden biri dayanmaktadır

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);
}