I have a calendar that posts the value in the following format
3 January, 2017
and when I convert it to carbon by doing
$carbon = Carbon::parse($data['due_date']);
echo $carbon;
I see 2016-01-03 20:17:00
My expected output is 2017-01-03
Try
$date = Carbon::createFromFormat('j F, Y', $data['due_date']);
Just use this 👇
echo date('Y-m-d', strtotime('3 January, 2017'));
I have several templates in which I need to write the literals for the day of the week and month. I created a small twig file:
{% set numDay = target.dateStop | date ("w", user_timezone) %}
{% set daysOfWeek = {0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'} %}
{% set weekDay = daysOfWeek[numDay] %}
{% set numMonth = target.dateStop | date ("n", user_timezone) %}
{% set months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} %}
{% set month = months[numMonth] %
In the template where I need these values I first include the "helper" template
{% include '#.../Email/HTML/en_US/localized_day_and_month.twig' with [user_timezone] %}
Then simply use the variable
... {{ weekDay }} ...
I still get this error:
failed: Variable "weekDay" does not exist in ".../HTML/it_IT:do_deadline_new.html.twig" at line 9
I know there's probably a better solution to my "localized date problem" but that's not my point here
EDIT
I moved the first snippet inside the base class I inherit in every template, but those variables still seem to be ignored:
base.html.twig
{% block dateTimeVariables %}
{% set numDay = target.dateStop | date ("w", user_timezone) %}
{% set daysOfWeek = {0: 'Domenica', 1: 'Lunedì', 2: 'Martedì', 3: 'Mercoledì', 4: 'Giovedì', 5: 'Venerdì', 6: 'Sabato'} %}
{% set weekDay = daysOfWeek[numDay] %}
{% set numMonth = target.dateStop | date ("n", user_timezone) %}
{% set months = {1: 'gennaio', 2: 'febbraio', 3: 'marzo', 4: 'aprile', 5: 'maggio', 6: 'giugno', 7: 'luglio', 8: 'agosto', 9: 'settembre', 10: 'ottobre', 11: 'novembre', 12: 'dicembre'} %}
{% set month = months[numMonth] %}
{% endblock %}
And in the child template:
{% extends '#.../Email/HTML/it_IT/base.html.twig' %}
And yet:
failed: Variable "weekDay" does not exist in ".../HTML/it_IT:do_deadline_new.html.twig" at line 6
The line 6 is this:
[{{ group.name }}] Today at {{ target.created | date("H:i", user_timezone) }} {{ agent.name }} {{ agent.surname }} has created a file due on {{ weekDay | lower }} {{ target.dateStop | date ("d", user_timezone) }} {{ month }} at {{ target.dateStop | date ("H:i", user_timezone) }}.
From twig doc :
The include statement includes a template and returns the rendered content of that file into the current namespace.
AFAIK the included file will only be rendered , any variable that had been set in the included file will not be added to the parent namespace.
So try to use extends instead. Something like this :
parent.twig
{% set numDay = target.dateStop | date ("w", user_timezone) %}
{% set daysOfWeek = {0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'} %}
{% set weekDay = daysOfWeek[numDay] %}
{% set numMonth = target.dateStop | date ("n", user_timezone) %}
{% set months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} %}
{% set month = months[numMonth] %}
child.twig
{% extends 'parent.twig'%}
I try do date_diff last month, but have a problem if month have 31 day he say I'm to 01 at this month. And if do 2 month later give good result. But again every month who have 31 days give result to 01.
To be more clear i example you:
{{ "now" | date("Y-m-d") }} {# 2015-07-31 #}
{{ "now" | date_modify("-1 month") | date("Y-m-d") }} {# 2015-07-01 #}
{{ "now" | date_modify("-2 month") | date("Y-m-d") }} {# 2015-05-31 #}
{{ "now" | date_modify("-3 month") | date("Y-m-d") }} {# 2015-05-01 #}
Any1 have any ideea why ? Because any if modify month with even number you go at month start not to the end of previous month.
Based on the answer here it seems that when PHP modifies a DateTime() object with the string '-1 month' it simply decrements the month value, if I understand correctly.
So given your example, you start with today's date: 2015-07-31.
PHP changes this to 2015-06-31. However, there are only 30 days in June. So it increments this up to the next date that makes sense, which is... 2015-07-01.
I tried replicating this with:
echo (new DateTime())->sub(new DateInterval('P1M'))->format('Y-m-d');
and:
$dt = new DateTime();
$dt->modify('-1 month');
echo $dt->format('Y-m-d');
and I got the exact same result in each case:
2015-07-01
So I guess it's just one of PHP's foibles. Pretty messy, a lot can happen in a day!
I was wondering the best way to do something. My controller makes a call to a service's function. This function obtains some DOMDocument stuff and I didnt want to pass this whole response to twig. So what I do is this
public function terminalService($terminal_command)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$responseData = $this->commandsService->executeSingleCommand($terminal_command);
$dom->loadXML($responseData);
$elements = $dom->getElementsByTagNameNS('http://www.test.com/ter_0', 'Text');
$elNum = 0;
$flightInfo = array();
while ( $elNum < $elements->length ) {
$flightInfo[] = $elements->item($elNum)->nodeValue;
++$elNum;
}
return $flightInfo;
}
So that takes the DOMDocument, finds all the elements which have the Text namespace and places them into an array element. My controller then receives this array and then passes it to the view
$commandData = $apiService->terminalService($terminal_command);
return $this->render('NickAlertBundle:Page:terminal.html.twig', array(
'data' => $commandData,
));
How can I get each element of the array displayed on a new line? In my view I am currently doing
<div class="col-md-8" id="terminal-window">
{% if data is defined %}
{% for d in data %}
{{ d|nl2br }}
{% endfor %}
{% endif %}
</div>
All that does however is display everything in one long string. funny enough, if I dump data it displays it how I want (but I obviously cant do this in production). So how can I display each array element on its own line?
Update
A dump displays the data how I want it displays e.g.
6 => "3*A$NZ9835 C4 D4 J4 Z4 B7 H7 M7 Y7 LHRLAX 1035 1350 * 777 0E"
7 => " Q7 T7 V7 W7 G7 K7 L7 S7 "
So as you can see line 7 is displayed with a lot of spaces in order to format the output correctly. If I simply place a break at the end of my output, it displays like so
7 #AY4014 F9 A9 P9 J9 C9 D9 I9 Y9 LHRLAX 1215 1530 * 77W 0E
B9 H9 K9 M9 L9 V9 S9 N9 Q9 O9 G9
Is there any way to retain the spaces or duplicate what a dump does?
Thanks
Since you have most of the logic set, I would change the for loop to this:
<div class="col-md-8" id="terminal-window">
{% if data is defined %}
{% for d in data %}
<pre>{{ d }}</pre><br />
{% endfor %}
{% endif %}
</div>
How to convert a datetime HH:MM:SS:mmm in SS.mmm with a filter in Twig please ( `` )?
exemple :
00:01:30.600 => 90.600
I tried that {{ Object.time | date("s") }} but it doesn't work ...
thanks !
As #john Smith told in his comment (unfortunately I can't +1 but he deserves it), you can use:
{{ Object.time | date("s.u") }}
To change microseconds to miliseconds, you have 2 choices:
Round to 3 digits:
{{ Object.time | date("s.u") | round(3) }}
Slice the 3 last chars:
{{ Object.time | date("s.u") | slice(0, -3) }}