Now I will give a very simple way of how to do localized date formatting in the Laravel framework in a project. In the end, we get the result, in which getting the property of the model, for example, created_at (timestamp format: 2015-10-20 19:45:56) - is converted to "October 20 at 19:45". Formatting can be customized to taste or based on needs.
Creating a file with translations
Create date.php in the app / lang / ru directory (if the current language is Russian). Its content will be the format of the date output with time and the correct end for months.
<?php return [ 'later' => ':date :time', 'today' => ' :time', 'yesterday' => ' :time', 'month_declensions' => [ 'January' => '', 'February' => '', 'March' => '', 'April' => '', 'May' => '', 'June' => '', 'July' => '', 'August' => '', 'September' => '', 'October' => '', 'November' => '', 'December' => '' ] ];
For English: <?php return [ 'later' => ':date at :time', 'today' => 'today at :time', 'yesterday' => 'yesterday at :time', 'month_declensions' => [ 'January' => 'January', 'February' => 'February', 'March' => 'March', 'April' => 'April', 'May' => 'May', 'June' => 'June', 'July' => 'July', 'August' => 'August', 'September' => 'September', 'October' => 'October', 'November' => 'November', 'December' => 'December' ] ];
')
Creating a library
Now we are implementing a library that will execute all of our logic for date conversion.
Create the app / libraries folder (if you don’t have one) and the DateFormat.php file in the Date folder, the full path will be app / libraries / Date / DateFormat.php.
<?php namespace Date; class DateFormat { public static function post($time) { $timestamp = strtotime($time); $published = date('dmY', $timestamp); if ($published === date('dmY')) { return trans('date.today', ['time' => date('H:i', $timestamp)]); } elseif ($published === date('dmY', strtotime('-1 day'))) { return trans('date.yesterday', ['time' => date('H:i', $timestamp)]); } else { $formatted = trans('date.later', [ 'time' => date('H:i', $timestamp), 'date' => date('d F' . (date('Y', $timestamp) === date('Y') ? null : ' Y'), $timestamp) ]); return strtr($formatted, trans('date.month_declensions')); } } }
Autoload libraries
In order for all libraries to be available for use, we add autoloading classes from our app / libraries directory to composer.json:
{ "autoload": { "classmap": [ "app/libraries" ] } }
And update the composer'a cache:
composer dump-autoload
Basic model
Here you can do in several ways:
- Create an abstract base model and inherit from it;
- Create a type (trait) and add its use to our models.
I chose the first method, since it seemed more practical.
In the created app / models / Model.php file, we add a method that will be called when the created_at property is received.
It will be called automatically, we do not need to do this.
<?php use Date\DateFormat; use Illuminate\Database\Eloquent\Model as Eloquent; abstract class Model extends Eloquent { public function getCreatedAtAttribute($attr) { return DateFormat::post($attr); } }
This code speaks for itself: when getting the model property created_at (the attribute name is converted to camelCase), return the value processed by the post method using our DateFormat library. You can also create methods for the updated_at, deleted_at, and other attributes.
Now you can check everything in practice, below is an example of a model and controller for all posts.
Model app / models / Post.php:
<?php class Post extends Model { protected $table = 'posts'; }
App / controllers / PostController.php controller
<?php class PostController extends Controller { public function index() { return Post::all(); } }
Let us turn to our controller (do not forget to prescribe the routing conditions) and get the output array of our posts in json-format, where the created_at property will be formatted in the required language and format.
You can implement such functionality using ServiceProvider, which will be an even easier way. But this method is clearer.
I would be glad to hear comments on the code. I would like to know what are the best practices for solving such problems.