I am using Laravel 5.2 with Carbon and I trying to get dates in locale format. I mean if I have the date 2015-12-31 in the database, I want it to be printed as 12/31/2015 or 31/12/2015 depends on the user locale.
In the User model I have an attribute locale that presents the user locale. But I don't know who to make it works. I know that Laravel using Carbon for dates, but in the Carbon class I see that the format Y-m-d is hard coded.
I still need the dates will be saved in format Y-m-d and only the presentation will depends on the User->locale attribute.
So how can I achieve that behavior? Is there any way to do that with Carbon itself?
I have solution for you.
Override getAttribute method in your model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use DateFormatting;
protected $dates = [
'finished_at',
// other field names that you need to format
];
public function getAttribute($key)
{
if ( array_key_exists( $key, $this->getDates() ) ) {
// here you can format your date
}
return parent::getAttribute($key);
}
}
after all you can access to this fields as usual(using magic __get())
$model->finished_at;
There is a way to do this using Carbon but it seems like it's really going to vary according to the specs of your server and what not. From reading, it seems like locale packages aren't always consistent. Use locale -a on your production server terminal to check which supported locales you have installed. If you need to install one, use sudo locale-gen <NEW LOCALE>.
But once you have your locale strings sorted out, it seems that you can say
$date = new Carbon;
setlocale('en_GB');
echo $date->formatLocalized('%x'); // '16/06/16'
I derived the %x modifier from the strftime() function, it specifies the format to be "the preferred date representation based on locale, without the time" according to the php manual
Related
When generating a UUID in Laravel, It's being generated according to the standard format which includes 4 dashes (hyphens) like this (for example):
51a0cb84-8b3d-43c4-bfd4-8fcef1f360d4
How to generate the UUID in Laravel without dashes or hyphens? like this:
51a0cb848b3d43c4bfd48fcef1f360d4
In Laravel (since version 5.6), you can generate a UUID (version 4) that follows the standard format using the Str::uuid() method:
use Illuminate\Support\Str;
return Str::uuid();
Or, to generates a "timestamp first" UUID that may be efficiently stored in an indexed database column, you may use Str::orderedUuid():
use Illuminate\Support\Str;
return Str::orderedUuid();
And because Laravel actually makes use of ramsey/uuid package to generate the UUID, then generating the UUID in Laravel without dashes or hyphens could be done using the package's getHex() method:
use Illuminate\Support\Str;
return Str::uuid()->getHex();
// OR
return Str::orderedUuid()->getHex();
i have time column in my data base . i need to defined field for this column.
this is my migration:
$table->date('date');
$table->time('time_begin');
$table->time('time_Enable');
this is my resource:
public function fields(Request $request)
{
return [
datetime::make('time_Enable'),
datetime::make('time_begin'),
];
}
i just need time not date and time
Impressively enough, Nova not only doesn't have a Time field but also doesn't support a time-only usage of DateTime field.
On the other side, Nova has plenty of third parties componentes that can be easily added via composer. I did look for some on Nova Packages and found two viable options: Laraning Time Field and Michielfb Time Field, they seem to be pretty good and open-source.
Nonetheless, they have relatively few stars on GitHub and a rather old latest version, which is not much of a problem for such a simples functionality, but I'd rather go with a native one, so here is how I implemented it:
Text::make('Duration')
->placeholder('##:##:##')
->rules('date_format:"H:i:s"')
->help('hh:mm:ss'),
So basically I used the Text field with a format validation. Also added the placeholder and help attributes to make it a bit more intuitive. It works well! and I'm using it with a time database field. Other colleges suggested using a masked component but it would also require a third party one and I didn't find a good option.
From the docs
You may customize the display format of your DateTime fields using the format method. The format must be a format supported by Moment.js:
DateTime::make('time_Enable')->format('H mm'),
DateTime::make('time_begin')->format('H mm'),
You can use the extraAttributes option with type key:
Text::make('time_begin')->withMeta(['extraAttributes' => ['type' => 'time']])
I had to use php artisan scout:import command, while I was developing the website in my local environment and everything worked perfectly. However, once I deployed my app and used the same command using ssh I got my model imported, but with int attributes rendered as strings, f.e. 1 was formatted as "1". Because of that, I fail to use numeric_filters. I use the MySql database in both cases.
Numbers are rendered as the string in JSON response by default.
You could either use instructions from this https://stackoverflow.com/questions/31527050/laravel-5-controller-sending-json-integer-as-string or you could use casts attributes in the model so every time model is serialized it will cast respective columns as specified.
E.g. In model, you can define something like this:
protected $casts = [
'something' => 'float'
];
Do you know of any way to check the current time in Joomla considering the selected time zone in global configuration?
I have been looking in administrator settings but did not see any single word about the current time.
You can use the following code:
$date = new DateTime();
$config = JFactory::getConfig();
$date->setTimezone(new DateTimeZone($config->get('offset')));
This does two things. The first line creates a DateTimeobject based on the current server time and timezone. The following two lines get Joomla's global configuration and change the timezone of out date object to that timezone.
You can then format the date to whatever format you like by calling $date->format('Y-m-d H:i:s');. You can replace the format specifier by whatever you need, a reference of possible formatting options can be found here: http://www.php.net/manual/de/function.date.php
I have a datetime stored in mysql as timestamp.
I then format the datetime using
$newdate = date('d M Y', strtotime($this->query->datetime));
My question is how do I translate the date using codeigniter builtin lang helper?
in the application language folder add a subfolder for the language you want to translate to. In that folder make a file called date_lang.php and handle all you date translations.
$lang['datefrom'] = "dateTo";
Another option for translation of dates is to use locale.
PHP will handle the date translations for you. Set the locale globally for the user.
setlocale(LC_ALL, 'en_UK.utf8');
In my current project, we use the locale to handle money and dates. We use CI language files to handle string translations