Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to change the user's timezone in Laravel 4?

Tags:

php

laravel

At the moment I have saved the users timezone in their database row and each time I print a date I am converting it to the user's timezone. How can I do this in a DRY manner?

Should I override where Eloquent returns a Carbon DateTime object. If so should I put this in a trait as I have below so I only have to write it once?

<?php

use Carbon\Carbon;
use Illuminate\Database\Eloquent;

trait ConvertTimeZone {
/**
 * Return a timestamp as DateTime object.
 *
 * @param  mixed  $value
 * @return DateTime
 */
protected function asDateTime($value)
{
    // If this value is an integer, we will assume it is a UNIX timestamp's value
    // and format a Carbon object from this timestamp. This allows flexibility
    // when defining your date fields as they might be UNIX timestamps here.
    if (is_numeric($value))
    {
        return Carbon::createFromTimestamp($value);
    }

    // If the value is in simply year, month, day format, we will instantiate the
    // Carbon instances from that fomrat. Again, this provides for simple date
    // fields on the database, while still supporting Carbonized conversion.
    elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value))
    {
        return Carbon::createFromFormat('Y-m-d', $value);
    }

    // Finally, we will just assume this date is in the format used by default on
    // the database connection and use that format to create the Carbon object
    // that is returned back out to the developers after we convert it here.
    elseif ( ! $value instanceof DateTime)
    {
        $format = $this->getDateFormat();

        $timezone = \Auth::user()->timezone;

        return Carbon::createFromFormat($format, $value, $timezone);
    }

    return Carbon::instance($value);
}

}

like image 607
Thomas Clarkson Avatar asked Jul 01 '13 00:07

Thomas Clarkson


1 Answers

I would create a BaseModel class, extending Eloquent, from which I'd extend the models I need such functionality from. Just have to remember to check if the user is logged in, so that we can get its timezone. Example:

models/BaseModel.php

class BaseModel extends Illuminate\Database\Eloquent\Model {

    protected function asDateTime($value) {

        // If Carbon receives null, it knows to use the default timezone
        $tz = null;

        // If the user is logged in, get it's timezone
        if (Auth::check()) {
            $tz = Auth::user()->timezone;
        }

        // If this value is an integer, we will assume it is a UNIX timestamp's value
        // and format a Carbon object from this timestamp. This allows flexibility
        // when defining your date fields as they might be UNIX timestamps here.
        if (is_numeric($value)) {
            return Carbon::createFromTimestamp($value, $tz);
        }

        // If the value is in simply year, month, day format, we will instantiate the
        // Carbon instances from that fomrat. Again, this provides for simple date
        // fields on the database, while still supporting Carbonized conversion.
        elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
            return Carbon::createFromFormat('Y-m-d', $value, $tz);
        }

        // Finally, we will just assume this date is in the format used by default on
        // the database connection and use that format to create the Carbon object
        // that is returned back out to the developers after we convert it here.
        elseif ( ! $value instanceof DateTime) {
            $format = $this->getDateFormat();

            return Carbon::createFromFormat($format, $value, $tz);
        }

        return Carbon::instance($value);
    }
}

models/User.php

class User extends BaseModel {
    // ...
}
like image 189
rmobis Avatar answered Oct 26 '22 23:10

rmobis