Pergunta

Please can anybody help !!

I want the date to be in the following format

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

but I am getting it in

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

I am using

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

The problem is jQuery is returning me date with GMT+0530 and PHP is returning it with Asia/Kolkata+0530

Edit

I am placing a timer in my code, which displays logged in since, so on login I set the current date time in session using PHP date function as written above , but the problem is the new Date() function of jQuery returns date in format other than the date in PHP. please find the code below

PHP Code:

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 Code: //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);
        }
    });
Foi útil?

Solução

There a tow different ways in PHP you can solve your problem. Just use the date and time related extensions like this:

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

The second example solving your problem:

$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");

For further examples just have a look for the php time and date related objects and functions: http://www.php.net/manual/en/book.datetime.php

Outras dicas

If you want to force GMT+0000, then hard code it:

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

As far as IST vs India Standard Time, I don't think there is a simple way.

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

Using T instead of E might be easier if you want to do a search/replace map.

Well I resolved it with the worst way possibly

$date = str_replace('Asia/Kolkata+0530', 'GMT+0530', date('D M d Y H:i:s eO (T)'));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top