I'm trying to convert this:
{{ "26/03/2013"|date("d/m/Y") }}
in Twig but its is throwing the error
Uncaught Exception: DateTime::__construct(): Failed to parse time
string (26/03/2013) at position 0 (2): Unexpected character in
/home/vagrant/Code/Phantom
Website/vendor/twig/twig/lib/Twig/Template.php on line 218.
If I pass this:
{{ "03/26/2013"|date("m/d/Y") }}
It works, so I imagine I need to change something related to Twigs date formatting
The date filter is about formatting DateTime Object, so if you pass a string this will be passed to the constructor of the DateTime object then to the format method, so in your case, you need to format the string that looks good for a DateTime constructor as example
{{ "2013-3-26"|date("d/m/Y") }}
From the doc:
The format specifier is the same as supported by date, except when the
filtered data is of type DateInterval, when the format must conform to
DateInterval::format instead.
And also about string format:
The date filter accepts strings (it must be in a format supported by
the strtotime function), DateTime instances, or DateInterval
instances. For instance, to display the current date, filter the word
"now":
Try this in this twigfiddle
If you use /'s as delimiter the expected format is m/d/Y,
To pass the date as day, month, year you need to use a - as delimiter
{{ "26-03-2017" | date('d/m/Y') }}
fiddle
Related
in my controller i used Carbon to get current timestamp like showing below:
$current_timestamp = Carbon::now()->format('j/n/Y');
the output of the above:
18/8/2022
and i am getting data from external API like showing below (from blade):
$data[0]['DocDate']
the output of the above:
18/8/2022 12:00:00 AM
now i want to remove 12:00:00 AM from it
i tried in blade view to do:
{{Carbon\Carbon::parse($data[29]['DocDate'])->toDateString()}}
but i am getting this error:
Could not parse '18/8/2022 12:00:00 AM': Failed to parse time string (18/8/2022 12:00:00 AM) at position 0 (1): Unexpected character
and i tried:
$data[29]['DocDate']->format('j/n/Y')
and i get this error:
Call to a member function format() on string
how can i overcome this issue?
You can use create from format function to change the format of incoming date as below:
$inDate = $data[0]['DocDate'];
$outDate = Carbon::createFromFormat('d/m/Y h:i:s a', $inDate )->format('d/m/Y');
On the principle of Keep It Simple - Why not just compare them as strings?, the longer one can easily be shortened, then just compare.
Accepted this is not a "purest" approach, but the format of a "standard" date is not likely to change.
now i want to remove 12:00:00 AM from it
So you don't actually care about date, you don't need Carbon or parsing according to format, you just need to keep the first word of your string:
{{ explode(' ', $data[0]['DocDate'])[0] }}
I am trying to get the time between two dates in diffForHumans format, can anyone help me out to why its throwing this error?
Code:
{{ Carbon\Carbon::createFromTimestamp($clientLogin->exit_timestamp)->diffForHumans($clientLogin->enter_timestamp) }}
Error:
Type error: Argument 1 passed to Illuminate\Cookie\Middleware\EncryptCookies::encrypt() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\View\View given
Suggestion: Define your datetime fields as dates so Laravel can treat then already as a carbon instance, so you can do something like this:
$clientLogin->exit_timestamp->subDays($clientLogin->enter_timestamp)->diffForHumans();
In the database, I have following date: 1924-01-17, as a date-type, but when I render it as {{ person.BirthDate | date("d/m/Y") }}, it gives as a result 17/01/2024.
How can I solve the Year-problem?
Twig documentation for date says:
The date filter accepts strings (it must be in a format supported by
the strtotime function), DateTime instances, or DateInterval
instances.
DateTime instance should do the trick, and based on comments it fixed this.
I using Twig in a PHP application,
The PHP object that I use has an attribute called "date", got from SQL Server.
It's look like : "Mar 2 2014 12:00:00:000AM"
I try to convert it using Twig for display it, I try with | date("Y-m-d") without success :
An exception has been thrown during the rendering of a template ("DateTime::__construct() [function.DateTime---construct]: Failed to parse time string (Jun 20 2013 12:00:00:000AM) at position 20 (:): Unexpected character") in "..." at line 96.
Any ideas ?
Thanks,
Have a nice day.
The date filter can work on \DateTime instances and strings that can be passed to strtotime(), apparently yours isn't.
You mention you're using an object that has the date property, I recommend adding a new function to it:
public function getDateAsObject()
{
// should be able to parse this format: Mar 2 2014 12:00:00:000AM
return \DateTime::createFromFormat("M j Y h:i:s:uA", $this->date);
}
You may need to adapt the format and the functions name for your conventions.
You can use it in your template:
{{ your_object.dateAsObject|date("Y-m-d" }}
You don't need to use date('Y-m-d'), base on the error message your object's date attribute is a type of DateTime, so use following:
// I assume 'object' is your object which has 'date' as the attribute
<span>Date: </span>{{ object->date->format('Y-m-d') }}
Check documentation for DateTime::Format
I am using Twig and this date filter
http://www.twig-project.org/doc/templates.html#date
Apparently they are looking out for DateTime instances in the parameter.
looking at this http://www.php.net/manual/en/datetime.construct.php
I have trouble understanding the php datetime object and how to use the timezone.
Given that i know basic PHP and is familiar with simple web programming, how do I use it to display a date and time using the Twig date filter while catering for timezone?
If there is a simpler way to do it while using the date filter, but NOT using datetime object, i would be open to it.
I am only concerned that the solution works, rather than the "correctness" or "elegance" of the solution.
The "Date" filter of Twig accept a second parameter: "timezone".
So, you can easily display all timezone that you want. For example:
{{ "now"|date("m/d/Y H:i", "Europe/Paris") }}
{{ "now"|date("m/d/Y H:i", "Asia/Calcutta") }}
{{ "now"|date("m/d/Y H:i", "Europe/Berlin") }}
For more informations:
http://twig.sensiolabs.org/doc/filters/date.html#timezone
In today's version, it has been supported in symfony application config file:
twig:
date:
timezone: Asia/Tokyo
http://symfony.com/blog/new-in-symfony-2-7-default-date-and-number-format-configuration
i know the question is old, but this is for reference.
By default Twig is going to use the default timezone that is set in php ini file or in the application globally, or the declared in twig.
if you pass a datetime object to the date filter with timezone , then you can pass false as a second argument in date filter.
{{ dateForSomething | date('theFormatIWant', false) }}
please refer to documentation
twig date
I think you might have misread the documentation.
The date filter accepts any date format supported by DateTime and DateTime instances.
That means that you can just pass in things like "2011-01-20 12:00:00" OR a real DateTime Object.
But you don't have to deal with the object if you don't want do.
Now if you need that string to be displayed in a specifiy timezone I would set that timezone in php before passing it to twig
$x = new DateTime("2010-01-01 12:00:00");
$x->setTimezone(new DateTimeZone("The Timezone you need"));
// pass to twig
What worked for me is adding a new filter, so it always looks at the timezone within the DateTime obj.
Below the example with the DateTime object as a parameter.
I may be missing something by I don't understand why Twig is ignoring the DateTime Timezone part and using the default global one.
In the filters extension:
public function getFilters()
{
return array(
(...)
new \Twig_SimpleFilter('date_tz', array($this, 'dateTzFilter')),
);
}
and
public function dateTzFilter(\DateTime $dateTime)
{
return $datTime->format('desired_format');
}