Using DateTime and DateInterval functions in Twig - function

I have two objects in Twig:
{{ date }} is DateTime object
{{ interval }} is DateInterval object
I need to show date with interval added. How can I use DateObject php function like add() with this two objects?
I can't do this on controller side, I need interval to date in twig. :(

You can use date_modify.
In your case, it would go as:
{{ yourDate|date_modify(yourInterval)|date("m/d/Y") }}

Related

Nested Jekyll liquid objects to pull _data file by today's date

I have a Jekyll site with a _data directory full of files organized by date:
/_data/
mydata_2020-08-28.json
mydata_2020-08-29.json
mydata_2020-08-30.json
mydata_2020-08-31.json
I'd like to pull data from these files based on today's date, but I can't quite seem to get it right.
Let's say that I want to pull data from {{ site.data.mydata_2020-08-31.somefield }}, but I want to dynamically generate the date (YYYY-MM-DD) as today's date using {{ 'now' | date: "%Y-%m-%d" }}. I need to insert the date object into the data file object, but I can't find a way to do it.
I've tried:
{{ site.data.mydata_{{ 'now' | date: "%Y-%m-%d" }}.somefield }}
but it returns the truncated end of my liquid object:
.somefield
And I tried:
{{site.data.mydata_| append: 'now' | date: "%Y-%m-%d" | append:.somefield}}
which returns today's date:
2020-08-31
I also read through most of the documentation at https://shopify.github.io/liquid/, but maybe I'm overlooking something?
If you want to fetch data based on the date in your case, try with the following:
{% assign today_data = 'now' | date: "%Y-%m-%d" | prepend: 'mydata_' %}
# You can fetch data file with particular date as follows:
{{ site.data[today_data] }}
NOTE: .somefield can't be fetched in above approach.

Format Date value from database Laravel

I have a column named birth_date in MySQL database with a type of date.
Now, I would like to show it in my view in this format : May 23, 2008
I tried:
{{ $user->user_information->birth_date->format('m-d-Y') }}
it showed:
ErrorException (E_ERROR) Call to a member function format() on string
I also tried
{{ date('m-d-Y', $user->user_information->baptism_date) }}
it showed:
ErrorException (E_ERROR) A non well formed numeric value encountered
Can anyone assist me with the right formatting code?
You can use the date() function for this purpose like:
date('m-d-Y', strtotime($user->user_information->birth_date));
or you can also use Carbon for this purpose like:
{{ \Carbon\Carbon::parse($user->user_information->birth_date)->format('m-d-Y')}}
For May 23, 2008:
date('M d, Y', strtotime($user->user_information->birth_date));
Working code snippet
date() format list is available here
M - A short textual representation of a month, three letters
Try this code.
{{ Carbon\Carbon::parse($user->user_information->birth_date)->format('F d, Y') }}
you can use date function. try this code:
{{ date('F j, Y', strtotime($user->user_information->birth_date)); }}

How to change month number in month name in Carbon Laravel 5.4

I have a table with a period and sales column.
the period look like this
2017001
2017002
......
2017011
2017012
I use the Carbon extension to extract the month number into month name with
{{ Carbon\Carbon::parse(substr($saleCurrent->period, -2))->month }}
but i'm getting an error
DateTime::__construct(): Failed to parse time string (01) at position 0 (0): Unexpected character
This is because the month should be 1 not 01 ?
Use the CreateFromFormat method so that you can pass in the format your dates are stored in:
{{ Carbon\Carbon::createFromFormat('Ymd', $saleCurrent->period)->month }}

Laravel format DateTime from database result

My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS and display it on my view in a table cell. My database dates are called date_begin and date_end.
Better, when I get this dates from database, convert it to DD.MM.YYYY format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.
How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.
Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problem whatsoever.
If you're using Eloquent Models you can do it very easily:
1. Add the columns to the $dates property in your model class:
protected $dates = ['date_begin', 'date_end'];
This will ensure that the values get mutated to Carbon instances.
2. In your view files you can use the format method that Carbon offers like so:
<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}
<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}
<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}
There's no need to split the value in time and date, just show what you want from the DateTime value using whatever format you want.
If you're not using Eloquent models, then you can manually use Carbon to format your value like so:
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
I have used the PHP solution without Carbon:
Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin
to:
date('d-m-Y H:i:s', strtotime($item->date_begin))
After that, when you save it on the database add on the DB update:
date('Y-m-d H:i:s', strtotime($request->date_begin))
protected $casts = [
'inicio' => 'datetime:Y-m-d\TH:i'
,'entrega' => 'datetime:Y-m-d\TH:i'
];
you could use it for the datetime-local field. greed you. like me if work for you
// attributes that should be cast to native types
protected $casts = [
'email_verified_at' => 'datetime'
];
// get formatted datetime string for email_verified_at
public function getEmailVerifiedAttribute()
{
if ($this->email_verified_at) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $this->email_verified_at, 'UTC');
return $date->setTimezone($this->timezone->name)->isoFormat('LLLL');
} else {
return null;
}
}
as Laravel document way.

Combining two variables for use in Twig

I'm using Google app engine and I'm trying to set the value of a textarea based on the concatenation of two string variables. Let's say I have 4 items, and each item has multiple fields. So in my Python I'm passing the dictionary { 'value0': newValue }. I'm using a for loop (iterator value num) and I want to use in my HTML something equivalent to {{ value }}{{ num }} where the variable referenced is value0.
I've tried {{ value~num }} but nothing works. If I could use an array that would be even better - such as {{ value[num] }}