2013-10-09 57 views
0

請任何人都可以幫忙!日期函數格式php

我想要的日期是按以下格式

Wed Oct 09 2013 14:48:26 GMT+0530 (India Standard Time) //this one is what jquery Date(); returns 

但我我使用

date('D M d Y H:i:s eO (T)'); 

的問題得到它在

Wed Oct 09 2013 15:42:38 Asia/Kolkata+0530 (IST) 

是jQuery是返回我與GMT + 0530約會和PHP返回亞洲/加爾各答+ 0530

編輯

我把一個計時器在我的代碼,它顯示在自登錄,所以在登錄時我使用PHP日期函數來設置在會話中的當前日期時間寫上面,但問題是新的Date()函數的jQuery返回的日期格式不是PHP中的日期。請在下面找到

PHP代碼代碼:

date_default_timezone_set("GMT+0530"); 
$_SESSION['loggedin'] = date('D M d Y H:i:s eO (T)'); // returns Wed Oct 09 2013 15:42:38 Asia/Kolkata+0530 (IST) 

JS代碼://JS date function returns time as Wed Oct 09 2013 14:48:26 GMT+0530 (India Standard Time)

$(document).ready(function(){ 
     if($('#logged').val()!=''){ 
      var pageVisisted = new Date($_SESSION['loggedin']); 

      setInterval(function() { 
       var timeOnSite = new Date() - pageVisisted; 

       var secondsTotal = timeOnSite/1000; 
       var hours = Math.floor(secondsTotal/3600); 
       var minutes = Math.floor(secondsTotal/60) % 3600; 
       var seconds = Math.floor(secondsTotal) % 60; 
       var showtime = "Logged in Since "; 
       if(hours>0){ 
        showtime = showtime + hours + " hours and "; 
       } 
       showtime = showtime + minutes + " mins"; 
       document.getElementById('counter').innerHTML = showtime; 
      }, 1000); 
     } 
    }); 
+0

這可能對您有所幫助[http://stackoverflow.com/questions/19245556/error-in-showing-outlook-appointment-time-using-php](http://stackoverflow.com/questions/19245556/error-in-showing-outlook-appointment-time-using-php) –

+0

@KishorSubedi,沒有幫助,thnx btw –

+0

你究竟在做什麼?你沒有很好地描述這個問題。請同時顯示javascript代碼和php代碼。你要去哪個方向?我無法從你的問題中得知。 –

回答

1

在PHP中有兩種不同的方式可以解決您的問題。只要使用日期和時間相關的擴展是這樣的:

date_default_timezone_set('Europe/Berlin'); 
$datetime = new DateTime("now"); 

// result: Thu Oct 24 2013 16:34:14 Europe/Berlin+0200 (CEST) 
echo $datetime->format("D M d Y H:i:s eO (T)"); 

第二個例子解決您的問題:

$timezone = new DateTimeZone('Asia/Calcutta'); 
$datetime = new DateTime("now", $timezone); 

// result: Thu Oct 24 2013 20:04:14 GMT+0530 
echo $datetime->format("D M d Y H:i:s \G\M\TO"); 

對於進一步的例子只是看看對PHP的時間和日期相關的對象和函數: http://www.php.net/manual/en/book.datetime.php

+0

非常感謝你!我不知道日期時間,我只是一次又一次地只用於日期功能 –

+0

賞金將在5小時內頒發! –

0

嗯,我用最糟糕的方式解決它可能

$date = str_replace('Asia/Kolkata+0530', 'GMT+0530', date('D M d Y H:i:s eO (T)')); 
1

如果你想強制GMT+0000,然後硬編碼:

echo date('D M d Y H:i:s \G\M\TO (e)'); 

至於IST vs India Standard Time,我不認爲有一個簡單的方法。

$tz_map = [ 
    'IST' => 'India Standard Time' 
]; 
echo strtr(date('D M d Y H:i:s \G\M\TO (e)'), $tz_map); 

使用的T代替E可能會更容易,如果你想要做一個查找/替換地圖。

+0

但我想動態的時區,PHP日期正在返回亞洲/加爾各答+ 0530和jquery GMT + 0530,並在計算中給出的問題 –

+0

O是GMT時間,所以我認爲日期('DM d YH:i:s \ G \ M \ TO(T)')就是你想要的。 – iodragon

+0

謝謝,但日期時間的事情工作! –