0

I'm trying to set the timezone configuration on config/app.php base on my the system time on CentOS VM automatically.

'timezone' => 'America/New_York',

I've tried added these few lines of code on top of config/app.php

// Try #1
// $timeZone = shell_exec("date +\"%Z %z\" | awk '{print $1}'");
// dd($timeZone);  I got "EST\n"

// Try #2
// session_start();
// $timezone = $_SESSION['time'];
// dd($timeZone);

// Try #3
// $timezone = date_default_timezone_get()
// dd($timeZone);

None of these above seem to work.

How would one go about debugging this further?

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604
  • How about using [Carbon](https://carbon.nesbot.com/docs/#api-getters) ? Something like that `Carbon::createFromTimestamp(0, 'America/New_York')->getTimezone();` – cch Nov 26 '18 at 19:10
  • I tried this(https://i.imgur.com/ForViQq.png) I got : https://i.imgur.com/ramGmWS.png – code-8 Nov 26 '18 at 19:11
  • Check this link for more info on the error https://stackoverflow.com/questions/34978828/uncaught-reflectionexception-class-log-does-not-exist-laravel-5-2 - make sure you add this `use Carbon\Carbon;` to your file as well – cch Nov 26 '18 at 19:16

1 Answers1

2

You can try this:

$now = new DateTime();
$tz = $now->getTimezone();
$tz = $tz->getName();

If the above doesn't work I suggest you to look at Carbon;

Your config/app.php file will be something like:

<?php
# include Carbon
use Carbon\Carbon;

return [
    . 
    .
    . 
    |--------------------------------------------------------------------------
    | 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' => Carbon::createFromTimestamp(0, 'America/New_York')->timezone->getName(),
    .
    . 
    .

];

To test the output of the above do not die dump dd() inside your app/config.php you can make a new route:

Route::get('/test-timezone', function() {
    dd(\Config::get('app.timezone'));
});

Make sure to clear the cache to see the changes: php artisan config:cache

cch
  • 3,336
  • 8
  • 33
  • 61