2

Iam trying to convert UTC timezone to Singapore Time

    echo $Date->format('Y-m-d H:i:s');
    echo '<br>'; 
    echo with(new Carbon\Carbon($Date->format('Y-m-d H:i:s')))->tz('Asia/Singapore')->format('Y-m-d H:i:s');
    exit;

it works fine in Localhost but in aws server it displays utc Time for both...Both server and Localhost is set to UTC Time .. how do we fix this??

Your Friend
  • 1,119
  • 3
  • 12
  • 27
  • I don't want to be rude but why would you tagged all laravel versions? First of all your question has nothing to do with Laravel. Possibly because Carbon comes default in composer in laravel4 but laravel3 has nothing to do with your question. – serdar.sanri Mar 03 '16 at 07:09

4 Answers4

2

I assume that $Date in your case is DateTime object (or Carbon object). You can use PHP setTimeZone() and DateTimeZone:

$Date = new DateTime($user->updated_at); // sample DateTime creation - also $Date = $user->updated_at; would work here

echo $Date->format("Y-m-d H:i:s")."<br />";

$Date->setTimezone(new DateTimeZone('Asia/Singapore'));
echo  $Date->format("Y-m-d H:i:s")."<br />";

For me it generates:

2014-09-19 12:06:15
2014-09-19 20:06:15
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
1

I assumed that, the $Date variable you have is a Carbon (By default available in Laravel) object:

$Date->format('Y-m-d H:i:s');

Now to set the timezone you may use any one of these:

$Date->timezone = new DateTimeZone('Asia/Singapore');
$Date->timezone = 'Asia/Singapore';
$Date->tz = 'Asia/Singapore';

You may also set it from your app/config/app.php like:

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'Asia/Singapore',
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

Try this it will work

 $timestamp ='1411205843'  //Timestamp which you need to convert 

 $sourceTimezone = new DateTimeZone('UTC');

 $destinationTimezone = new DateTimeZone('Asia/Singapore'); 

 $dt = new DateTime(date('m/d/Y h:i A', $timestamp), $sourceTimezone);
 $dt->setTimeZone($destinationTimezone);

 echo strtotime($dt->format('m/d/Y h:i A'));
Satish Shinde
  • 2,878
  • 1
  • 24
  • 41
  • 1
    Why in the world would you mix up `DateTime` class with `date()` and `strtotime()` functions? This can be simply solved using only `DateTime` class... **[DEMO](https://eval.in/197869)** – Glavić Sep 24 '14 at 05:57
0

You can set to session like below;

 Session::put('timezone', 'Your/TimeZone');

Enjoy your Code !

Enver
  • 542
  • 5
  • 7